我使用关于GoogleHttpClientSpiceRequest的示例,但我需要以JSON格式发送请求。所以,我改变了我的代码,但是我收到了错误:
07-11 15:39:08.738: E//DefaultRequestRunner.java:138(5662): 15:39:08.744
Thread-11643 An exception occurred during request network execution :null
07-11 15:39:08.738: E//DefaultRequestRunner.java:138(5662): java.lang.NullPointerException 07-11 15:39:08.738:
E//DefaultRequestRunner.java:138(5662): at com.octo.android.robospice.sample.googlehttpclient.SampleSpiceRequest.loadDataFromNetwork(SampleSpiceRequest.java:43)
07-11 15:39:08.738: E//DefaultRequestRunner.java:138(5662): at com.octo.android.robospice.sample.googlehttpclient.SampleSpiceRequest.loadDataFromNetwork(SampleSpiceRequest.java:1)
07-11 15:39:08.738: E//DefaultRequestRunner.java:138(5662): at com.octo.android.robospice.request.CachedSpiceRequest.loadDataFromNetwork(CachedSpiceRequest.java:45)
07-11 15:39:08.738: E//DefaultRequestRunner.java:138(5662): at com.octo.android.robospice.request.DefaultRequestRunner.processRequest(DefaultRequestRunner.java:134)
07-11 15:39:08.738: E//DefaultRequestRunner.java:138(5662): at com.octo.android.robospice.request.DefaultRequestRunner$1.run(DefaultRequestRunner.java:201)
07-11 15:39:08.738: E//DefaultRequestRunner.java:138(5662): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:390)
07-11 15:39:08.738: E//DefaultRequestRunner.java:138(5662): at java.util.concurrent.FutureTask.run(FutureTask.java:234) 07-11
15:39:08.738: E//DefaultRequestRunner.java:138(5662): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
07-11 15:39:08.738: E//DefaultRequestRunner.java:138(5662): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
07-11 15:39:08.738: E//DefaultRequestRunner.java:138(5662): at java.lang.Thread.run(Thread.java:864)
我的代码: 包com.octo.android.robospice.sample.googlehttpclient;
import java.io.IOException;
import org.json.JSONException;
import org.json.JSONObject;
import roboguice.util.temp.Ln;
import android.util.Log;
import com.google.api.client.http.ByteArrayContent;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.json.jackson.JacksonFactory;
import com.octo.android.robospice.request.googlehttpclient.GoogleHttpClientSpiceRequest;
import com.octo.android.robospice.sample.googlehttpclient.model.json.WeatherResult;
public class SampleSpiceRequest extends GoogleHttpClientSpiceRequest< WeatherResult > {
private String baseUrl;
private JSONObject jsonObject;
public SampleSpiceRequest( String zipCode ) {
super( WeatherResult.class );
this.baseUrl = "my domain";
JSONObject jsonObject = new JSONObject();
try {
jsonObject.accumulate("action", "login");
jsonObject.accumulate("username", "root");
jsonObject.accumulate("password", "pass");
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public WeatherResult loadDataFromNetwork() throws IOException {
HttpRequest request = getHttpRequestFactory().buildPostRequest(
new GenericUrl(baseUrl),
ByteArrayContent.fromString("application/json", jsonObject.toString())
);
request.setParser(new JacksonFactory().createJsonObjectParser());
Log.i("DEBUG", request.execute().parseAsString());
return request.execute().parseAs(getResultType());
}
}
答案 0 :(得分:0)
您的本地变量JSONObject jsonObject
正在隐藏您的实例成员JSONObject jsonObject
。
更改构造函数以初始化实例成员:
public SampleSpiceRequest( String zipCode ) {
// ...
jsonObject = new JSONObject();
// ...
}
答案 1 :(得分:0)
你声明jsonObject两次:
这里,全球:
private JSONObject jsonObject;
在这里,本地
JSONObject jsonObject = new JSONObject();
try {
jsonObject.accumulate("action", "login");
jsonObject.accumulate("username", "root");
jsonObject.accumulate("password", "pass");
} catch (JSONException e) {
e.printStackTrace();
}
只有在SampleSpiceRequest
构造函数中才能看到一个decalred local,所以当你在loadDataFromNetwork中调用jsonObject.toString()
时,你引用的是private JSONObject jsonObject;
,这是为什么{{1} }}