Android Question Can not Read JSON Data from Web API

junaidahmed

Well-Known Member
Licensed User
Longtime User
First Time I am trying to read Json Data from Web API.but its through an exception.Please check below log error and advise how to resolve

** Service (httputils2service) Start **
ResponseError. Reason: Method Not Allowed, Response: {"Message":"The requested resource does not support http method 'POST'."}
Error occurred on line: 122 (Main)
org.json.JSONException: End of input at character 0 of
at org.json.JSONTokener.syntaxError(JSONTokener.java:449)
at org.json.JSONTokener.nextValue(JSONTokener.java:97)
at anywheresoftware.b4a.objects.collections.JSONParser.NextArray(JSONParser.java:60)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
at anywheresoftware.b4a.shell.DebugResumableSub$RemoteResumableSub.resume(DebugResumableSub.java:19)
at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:240)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:132)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
at anywheresoftware.b4a.keywords.Common$14.run(Common.java:1736)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
B4X:
Sub Activity_Create(FirstTime As Boolean)
    
    'Do not forget to load the layout file created with the visual designer. For example:
    
    
    Activity.LoadLayout("ProductionEntry")

    If FirstTime = True Then
        intKeyBoardMode = 1
        StrURLPath = "http://103.76.188.138:85/WebAPI/api/ProductionEntry/"
        
        SpinnerMap.Initialize
        
        Dim parser As JSONParser
        Wait For(ExecuteJson("OperationTable","GetOperationTable")) Complete (Result As String)
        parser.Initialize(Result)
        
        Dim root As List = parser.NextArray
        For Each colroot As Map In root
            CboTable.Add(colroot.Get("OperationTableName"))
            SpinnerMap.Put(colroot.Get("OperationTableName"), colroot.Get("OperationTableCode"))
        Next
        
        
        
    End If
    
    
End Sub


Sub ExecuteJson(StrJobName As String,StrAction As String) As ResumableSub
    
    Dim Job As HttpJob
    Dim StrJson As String
    Job.Initialize(StrJobName, Me)
    Job.PostString(StrURLPath & StrAction,"")
    Job.GetRequest.SetHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0")
    Job.GetRequest.SetContentType("application/json;charset=UTF-8")
    Wait For (Job) JobDone(j As HttpJob)
    If j.Success Then
        StrJson = j.GetString   
    End If
    Return StrJson
    
End Sub
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

junaidahmed

Well-Known Member
Licensed User
Longtime User
Actually I have used ASP.NET Web API 2. For that I have used Model and controller and also please check above URL for output of JSON

B4X:
Model:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace mPortWebAPI.Models
{
    public class OperationTableModel
    {
        public string OperationTableCode { get; set; }
        public string OperationTableName { get; set; }
      

    }
}

Controller :-

   [HttpGet]
        [ActionName("GetOperationTable")]

        public List<OperationTableModel> GetOperationTable()
        {
            using (DbManager Db = new DbManager())
            {
                return Db.SetCommand("Select OperationTableCode,OperationTableName from GloveERP.Dbo.Common_Ref_OperationsTable ").ExecuteList<OperationTableModel>();
                Db.Close();
                Db.Dispose();
            }
        }

WebAPI Config :-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web.Http;
using Microsoft.Owin.Security.OAuth;
using Newtonsoft.Json.Serialization;

namespace mPortWebAPI
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
          
            config.Routes.MapHttpRoute(
            name: "API Default",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional });
                      
        }
    }
}
 
Upvote 0

junaidahmed

Well-Known Member
Licensed User
Longtime User
Please help...

Actually I am using this web API in other Mobile apps (DevExtreme Framework) ...

I don't understand which kind of Documentation you required....
 
Upvote 0

junaidahmed

Well-Known Member
Licensed User
Longtime User
Still I have not solved my problem... I have removed "Job.PostString" and added Job.Download.Please check below log and advise the same.....

B4X:
Sub ExecuteJson(StrJobName As String,StrAction As String) As ResumableSub
  
   Dim Job As HttpJob
   Dim StrJson As String
   Job.Initialize(StrJobName, Me)
   Job.Download("http://103.76.188.138:85/WebAPI/api/ProductionEntry/GetOperationTable")
   Job.GetRequest.SetHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0")
   Job.GetRequest.SetContentType("application/json;charset=UTF-8")
   Wait For (Job) JobDone(j As HttpJob)
   If j.Success Then
       StrJson = j.GetString   
   End If
   Return StrJson
  
End Sub


Log Error :-

at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
** Activity (main) Resume **
--------- beginning of system
** Activity (main) Create, isFirst = true **
Error occurred on line: 143 (Main)
java.lang.RuntimeException: Request does not support this method.
at anywheresoftware.b4h.okhttp.OkHttpClientWrapper$OkHttpRequest.SetContentType(OkHttpClientWrapper.java:493)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:755)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:345)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
at camera.scanner.main.afterFirstLayout(main.java:104)
at camera.scanner.main.access$000(main.java:17)
at camera.scanner.main$WaitForLayout.run(main.java:82)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
** Activity (main) Resume **
 
Upvote 0
Top