Android Question android.os.NetworkOnMainThreadException

Devo Frihandana

New Member
my name is devo and i'm new with this B4a,
i'm on study task to make an application to search by id or name, but when i parse the data from server, sometimes i made it, but sometime i always get "android.os.NetworkOnMainThreadException" and i dont know why..

please any body can help me, coz i'm on deadline to collect


but before, i'm sorry to paste my code here, coz i dont know how to makelike editor view
hehe

and this the code that i made


#Region Project Attributes
#ApplicationLabel: JSON
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region

#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region

Sub Process_Globals
Dim HttpClient1 As HttpClient

End Sub

Sub Globals

Dim sJSONData As String
Dim Msg As String
Dim Label1 As Label
Dim ListView1 As ListView
Dim Load As Button
Dim Panel1 As Panel
Dim Panel2 As Panel
Dim Panel3 As Panel
Dim Panel4 As Panel
Dim Parse As Button
Dim Clear As Button
Dim Search As Button
Dim EditText1 As EditText
Dim Label2 As Label
Dim Panel5 As Panel
Dim Panel6 As Panel
Dim Spinner1 As Spinner
End Sub

Sub Activity_Create(FirstTime As Boolean)

HttpClient1.Initialize("HttpClient1")
Activity.LoadLayout("json1")
Spinner1.Add("No Id")
Spinner1.Add("Name")

End Sub

Sub HttpClient1_ResponseSuccess (Response As HttpResponse, TaskId As Int)
ProgressDialogHide
Dim result As String
result = Response.GetString("UTF8") 'Convert the response to a string
sJSONData = result

If (Search.Enabled==True) Then
LoadJSONDATA
End If

ToastMessageShow("Data Sudah Tersedia dan Dapat Dilihat", True)
End Sub

Sub HttpClient1_ResponseError (Reason As String, TaskId As Int)
ProgressDialogHide
Msg = "Error connecting to server."
If Reason <> Null Then Msg = Msg & CRLF & Reason
ToastMessageShow (Msg, True)
End Sub

Sub GetJSONData()
Dim request As HttpRequest
request.InitializeGet("http://lpbd.si.fti.unand.ac.id/tesdevo.php")
request.Timeout = 10000 'set timeout to 10 seconds
'If HttpClient1.Execute(request, 1) = False Then Return 'Will be false if their is already a running task (with the same id).
HttpClient1.Execute(request, 1)
ProgressDialogShow("Connecting To Server...")

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Parse_Click
LoadJSONDATA
Load.Enabled=False
Clear.Enabled=True
Parse.Enabled=False
Search.Enabled=True
Spinner1.Enabled=True
EditText1.Enabled=True
End Sub

Sub Load_Click
GetJSONData
Parse.Enabled=True
Load.Enabled=False
End Sub

Sub Clear_Click
ListView1.Clear
Parse.Enabled=True
Search.Enabled=False
Spinner1.Enabled=False
End Sub

Sub Search_Click

ListView1.Clear
Dim cari As String
Dim kata As String
Dim a As String
kata = EditText1.Text
cari = Spinner1.SelectedItem

If (cari=="No Id") Then
a = "id"
Else
a = "name"
End If

Dim req As HttpRequest
req.InitializeGet("http://lpbd.si.fti.unand.ac.id/caritesdevo.php?cari="&a&"&kata="&kata&"")

HttpClient1.Execute(req, 1)

'If HttpClient1.Execute(req, 2) = False Then Return
ProgressDialogShow("Please Wait")
'---------------------------------

End Sub

Sub LoadJSONDATA()

Dim JSON As JSONParser
Dim Map1 As Map

'mengambil data
JSON.Initialize(sJSONData)
Map1 = JSON.nextobject
Dim l1 As List
l1 = Map1.Get("result")
Dim m As Map


For i = 0 To l1.Size-1
m = l1.Get(i)
ListView1.AddSingleLine(m.Get("id"))
ListView1.AddSingleLine(m.Get("name"))
ListView1.AddSingleLine(m.Get("tlp"))
Next


End Sub
 

Attachments

  • json.zip
    405.6 KB · Views: 173

Devo Frihandana

New Member
thanks for your reply
B4X:
#Region Project Attributes
#ApplicationLabel: JSON
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region

#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region

Sub Process_Globals
Dim HttpClient1 As HttpClient

End Sub

Sub Globals

Dim sJSONData As String
Dim Msg As String
Dim Label1 As Label
Dim ListView1 As ListView
Dim Load As Button
Dim Panel1 As Panel
Dim Panel2 As Panel
Dim Panel3 As Panel
Dim Panel4 As Panel
Dim Parse As Button
Dim Clear As Button
Dim Search As Button
Dim EditText1 As EditText
Dim Label2 As Label
Dim Panel5 As Panel
Dim Panel6 As Panel
Dim Spinner1 As Spinner
End Sub

Sub Activity_Create(FirstTime As Boolean)

HttpClient1.Initialize("HttpClient1")
Activity.LoadLayout("json1")
Spinner1.Add("No Id")
Spinner1.Add("Name")

End Sub

Sub HttpClient1_ResponseSuccess (Response As HttpResponse, TaskId As Int)
ProgressDialogHide
Dim result As String
result = Response.GetString("UTF8") 'Convert the response to a string
sJSONData = result

If (Search.Enabled==True) Then
LoadJSONDATA
End If 

ToastMessageShow("Data Sudah Tersedia dan Dapat Dilihat", True)
End Sub

Sub HttpClient1_ResponseError (Reason As String, TaskId As Int)
ProgressDialogHide
Msg = "Error connecting to server."
If Reason <> Null Then Msg = Msg & CRLF & Reason
ToastMessageShow (Msg, True)
End Sub

Sub GetJSONData()
Dim request As HttpRequest
request.InitializeGet("http://lpbd.si.fti.unand.ac.id/tesdevo.php")
request.Timeout = 10000 'set timeout to 10 seconds
'If HttpClient1.Execute(request, 1) = False Then Return 'Will be false if their is already a running task (with the same id).
HttpClient1.Execute(request, 1)
ProgressDialogShow("Connecting To Server...") 

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Parse_Click
LoadJSONDATA
Load.Enabled=False
Clear.Enabled=True
Parse.Enabled=False
Search.Enabled=True
Spinner1.Enabled=True 
EditText1.Enabled=True
End Sub

Sub Load_Click
GetJSONData
Parse.Enabled=True
Load.Enabled=False
End Sub

Sub Clear_Click
ListView1.Clear
Parse.Enabled=True
Search.Enabled=False
Spinner1.Enabled=False
End Sub

Sub Search_Click

ListView1.Clear
Dim cari As String
Dim kata As String
Dim a As String
kata = EditText1.Text
cari = Spinner1.SelectedItem

If (cari=="No Id") Then
a = "id"
Else
a = "name"
End If

Dim req As HttpRequest
req.InitializeGet("http://lpbd.si.fti.unand.ac.id/caritesdevo.php?cari="&a&"&kata="&kata&"")

HttpClient1.Execute(req, 1)

'If HttpClient1.Execute(req, 2) = False Then Return
ProgressDialogShow("Please Wait") 
'---------------------------------

End Sub

Sub LoadJSONDATA()

Dim JSON As JSONParser
Dim Map1 As Map

'mengambil data
JSON.Initialize(sJSONData)
Map1 = JSON.nextobject
Dim l1 As List
l1 = Map1.Get("result")
Dim m As Map


For i = 0 To l1.Size-1
m = l1.Get(i)
ListView1.AddSingleLine(m.Get("id"))
ListView1.AddSingleLine(m.Get("name"))
ListView1.AddSingleLine(m.Get("tlp"))
Next


End Sub


how to use HttpUtils2 in my code ?
 
Upvote 0
Top