Home > Mobile >  The Android learning exchange
The Android learning exchange

Time:09-29

Empty time will come within the scope of the ability to help students answer questions, reply format:
1. Problem description
2. Key code + log

CodePudding user response:

1. The JSONObject jsonObj=new JSONObject (data). GetJSONObject (" serverinfo ");
The line
Why error
 String data=(https://bbs.csdn.net/topics/response.body). The String (); 
The d (" test ", "onResponse:" + response + "-- -- -- -- --" + data).
Try {
JSONObject jsonObj=new JSONObject (data). GetJSONObject (" serverinfo ");
Int id=jsonObj. Get int (" CarSpeed ");

Gson_json. SetText (" speed "+ id);
} the catch (JSONException e) {
System. The out. Println (" Json parse error ");
e.printStackTrace();
}

 D/test: onResponse: the Response {=HTTP/1.1 protocol, code=200, message=, url=http://192.168.31.120:8080/transportservice/type/jason/action/GetCarSpeed.do} - {" serverinfo ":" {12} \ "CarSpeed " : \ n "} 
I/System. Out: Json parse error
W/System. Err: org. Json. JSONException: Value {12} "CarSpeed" :
The at serverinfo of type Java. Lang. String always be converted to JSONObject
W/System. Err: at org. Json. Json. TypeMismatch (json. Java: 100)
The at org. Json. JSONObject. GetJSONObject (JSONObject. Java: 613)
At com. Example. Tomlin. Post_get. OkHttpClientActivity $2 $1. OnResponse (OkHttpClientActivity. Java: 167)
The at okhttp3. RealCall $AsyncCall. Execute (RealCall. Java: 153)
The at okhttp3. Internal. NamedRunnable. Run (NamedRunnable. Java: 32)
The at Java. Util. Concurrent. ThreadPoolExecutor. RunWorker (ThreadPoolExecutor. Java: 1113)
The at Java. Util. Concurrent. ThreadPoolExecutor $Worker. The run (ThreadPoolExecutor. Java: 588)
The at Java. Lang. Thread. The run (Thread. Java: 818)


CodePudding user response:

You have a look at your data print, serverinfo may lack escape character cannot be identified, give it a try

CodePudding user response:

reference 1st floor weixin_44090202 response:
1. The JSONObject jsonObj=new JSONObject (data). GetJSONObject (" serverinfo ");



Your serverinfo is string, haven't seen a quote, please {" serverinfo ":" {... }} ", get string first, then the string into jsonobj

CodePudding user response:

reference 3 floor wang can reply:
Quote: refer to 1st floor weixin_44090202 response:

1. The JSONObject jsonObj=new JSONObject (data). GetJSONObject (" serverinfo ");



Your serverinfo is string, haven't seen a quote, please {" serverinfo ":" {... }} ", get string first, then the string into jsonobj


Dubious, blind, I can't see, what I said,

CodePudding user response:

Could you tell me why the return value is the beginning of
 package com. Lenovo. Smarttraffic. Post; 
The import android. Util. Log;
import java.io.IOException;
The import okhttp3. MediaType;
The import okhttp3. OkHttpClient;
The import okhttp3. Request;
The import okhttp3. RequestBody;
The import okhttp3. The Response;

Public class Post {
Private String data="https://bbs.csdn.net/topics/original data".
Public String SendPost (url String, the String sendJson) {
New Thread () {
@ Override
Public void the run () {
The String data.
Super. The run ();
OkHttpClient client=new OkHttpClient ();
RequestBody body=RequestBody. Create (MediaType. Parse (" application/json; Charset=utf-8 "), sendJson);
Request the Request=new Request. The Builder (). The url (url). The post (the body). The build ();
The Response the Response=null;
Try {
The response=client. NewCall (request). The execute ();
Data=(https://bbs.csdn.net/topics/response.body). The string ();
The d (" access to the data value ", data);
} the catch (IOException e) {
e.printStackTrace();
}

}
}.start();

return data;
}

}

CodePudding user response:

The
reference 5 floor 瑓 瑆 response:
could you tell me why the return value is the beginning of
 package com. Lenovo. Smarttraffic. Post; 
The import android. Util. Log;
import java.io.IOException;
The import okhttp3. MediaType;
The import okhttp3. OkHttpClient;
The import okhttp3. Request;
The import okhttp3. RequestBody;
The import okhttp3. The Response;

Public class Post {
Private String data="https://bbs.csdn.net/topics/original data".
Public String SendPost (url String, the String sendJson) {
New Thread () {
@ Override
Public void the run () {
The String data.
Super. The run ();
OkHttpClient client=new OkHttpClient ();
RequestBody body=RequestBody. Create (MediaType. Parse (" application/json; Charset=utf-8 "), sendJson);
Request the Request=new Request. The Builder (). The url (url). The post (the body). The build ();
The Response the Response=null;
Try {
The response=client. NewCall (request). The execute ();
Data=(https://bbs.csdn.net/topics/response.body). The string ();
The d (" access to the data value ", data); nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related