我是Android Web服務的新手,我想與php服務器通信,我的響應代碼是200,但是Buffered Reader在readline上返回null。Android JSON響應
我不知道什麼是問題。好心幫我
我執行的AsyncTask任務使用:針對的AsyncTask
new AsysnchronousTask().execute("");
,完整的代碼如下
public class AsysnchronousTask extends AsyncTask<String, String, String> {
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
Log.d(" sever Resutl ", result);
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String result = "";
HttpClient mDefaultHttpClient = new DefaultHttpClient();
HttpPost mHttpPost = new HttpPost(PATH);
// JSONObject sendJsonObject= new JSONObject();
JSONObject postJsonObject = new JSONObject();
HttpResponse mResponse;
try {
try {
postJsonObject.put("email", "[email protected]");
postJsonObject.put("login", "alan");
postJsonObject.put("password", "120519");
postJsonObject.put("language", "en");
JSONArray mJsonArray = new JSONArray();
mJsonArray.put(postJsonObject);
mHttpPost.setHeader("REGISTER", postJsonObject.toString());
mHttpPost.getParams().setParameter("jsonpost", mJsonArray);
// post the data
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mResponse = mDefaultHttpClient.execute(mHttpPost);
StatusLine statuscod = mResponse.getStatusLine();
int statuscode = statuscod.getStatusCode();
Header[] mHeader= mResponse.getAllHeaders();
if (statuscode == 200) {
HttpEntity mEntity = mResponse.getEntity();
if (mEntity != null) {
InputStream mInputStream = mEntity.getContent();
// Converting Stream into the string
BufferedReader mBufferedReader = new BufferedReader(
new InputStreamReader(mInputStream));
StringBuilder mBuilder = new StringBuilder();
String line = null;
while ((line = mBufferedReader.readLine()) != null) {
mBuilder.append(line + "/n");
}
mBufferedReader.close();
mInputStream.close();
}
else {
Log.d(TAG, "Fail to Read from Server ");
}
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
JSONArray mJsonArray = new JSONArray(result);
JSONObject mJsonObject = null;
for (int i = 0; i < mJsonArray.length(); i++) {
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
}
http://android-spirit.blogspot.in/2013/08/cosume-phppost-method-webservice-in.html – Nirmal
謝謝尼日利亞..好的鏈接 – kami
歡迎開心編碼.... – Nirmal