gpt4 book ai didi

java - Json 响应未打印

转载 作者:太空宇宙 更新时间:2023-11-04 14:30:22 25 4
gpt4 key购买 nike

我是 Android 和 Java 开发新手。如果有人可以指导我,我将不胜感激:

我的代码:

public class TokenPost extends MainActivity {

public TokenPost(MainActivity mainActivity) {
// TODO Auto-generated constructor stub
}

public void postData() {

Thread thread = new Thread(new Runnable(){
@Override
public void run() {
try {

try{
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.traxbuddy.com/confirm.json");

Log.i("Inside TokenPost", "Inside TokenPost");
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(3);
//String foo= "id_token="+token+"&lat="+59.8944400+"&lng="+30.2641700;
//nameValuePair1.add(new BasicNameValuePair("id_token", token ));
Log.i("Mobile Token", "Mobile Token" + mtokenno);
nameValuePair.add(new BasicNameValuePair("mobile", "919876543285"));
nameValuePair.add(new BasicNameValuePair("imei", "911212345123891"/*imeicontact*/));
nameValuePair.add(new BasicNameValuePair("stoken", mtokenno));

// Execute HTTP Post Request



HttpResponse response = httpclient.execute(httppost);
Log.i("Response from Http Post Request", "Response from Http Post Request: "+ response);

System.out.println("Response: " +response.toString());

Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonParser jp = new JsonParser();
JsonElement je = jp.parse(response.toString());
String prettyJsonString = gson.toJson(je);
System.out.println(je);
System.out.println(prettyJsonString);

BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
StringBuilder builder = new StringBuilder();
for (line = null; (line = reader.readLine()) != null;) {
builder.append(line).append("\n");

System.out.println(line); }

}
catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}

} catch (Exception e) {
e.printStackTrace();
}
}
});

thread.start();


}//postData
}

我在日志中得到的输出是:

10-07 13:28:49.803: I/Inside TokenPost(1312): Inside TokenPost
10-07 13:28:49.803: I/Mobile Token(1312): Mobile Tokennull
10-07 13:29:02.573: I/Response from Http Post Request(1312): Response from Http Post Request: org.apache.http.message.BasicHttpResponse@b20e3f18
10-07 13:29:02.573: I/System.out(1312): Response: org.apache.http.message.BasicHttpResponse@b20e3f18
10-07 13:29:02.603: I/System.out(1312): "org.apache.http.message.BasicHttpResponse@b20e3f18"
10-07 13:29:02.603: I/System.out(1312): "org.apache.http.message.BasicHttpResponse@b20e3f18"
10-07 13:29:02.603: W/System.err(1312): java.lang.NullPointerException
10-07 13:29:02.613: W/System.err(1312): at com.traxbuddy.trax.TokenPost$1.run(TokenPost.java:68)
10-07 13:29:02.613: W/System.err(1312): at java.lang.Thread.run(Thread.java:841)

我应该得到的输出(我通过使用 CURL 得到它):

{"atoken":"11528aa467271341a624f362ee7dd268b0366016","utoken":"c6205d52218f6d466
e2c4bac3ecc585ddab9db96"}

在这方面的任何帮助将不胜感激。

最佳答案

我的猜测是 response.getEntity().getContent() 返回 null,从而触发 NPE。

您是否尝试过使用以下方法反序列化主体:

import org.apache.http.util.EntityUtils;
// ...
String json = EntityUtils.toString(response.getEntity(), "UTF-8");

关于java - Json 响应未打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26241918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com