我是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"}
在这方面的任何帮助都将受到高度赞赏。
答案 0 :(得分:0)
我的猜测是response.getEntity().getContent()
返回null
,触发NPE。
您是否尝试使用此方法反序列化正文:
import org.apache.http.util.EntityUtils;
// ...
String json = EntityUtils.toString(response.getEntity(), "UTF-8");
答案 1 :(得分:0)
您正在尝试将response.toString()
解析为"org.apache.http.message.BasicHttpResponse@b20e3f18"
,作为JsonElement,触发异常。
替换
JsonElement je = jp.parse(response.toString());
与
JsonElement je = jp.parse(EntityUtils.toString(response.getEntity()), "UTF-8");