2013-10-09 121 views
1

我是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; 

     } 

    } 
+0

http://android-spirit.blogspot.in/2013/08/cosume-phppost-method-webservice-in.html – Nirmal

+1

謝謝尼日利亞..好的鏈接 – kami

+0

歡迎開心編碼.... – Nirmal

回答

0

我看到了幾個方法,你甚至可以前提高你的代碼試圖解決你的響應問題。我建議你在發送你的信息時實現一個BasicNameValuePair ArrayList而不是JSON。在你的情況下,實現如下

enter code InputStream is = null; 
String result = ""; 
String key = ""; 
ArrayList<SharePrices> results = new ArrayList<SharePrices>(); 

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
nameValuePairs.add(new BasicNameValuePair("email", "[email protected]")); 
nameValuePairs.add(new BasicNameValuePair("login", "alan")); 
nameValuePairs.add(new BasicNameValuePair("password", "120519")); 
nameValuePairs.add(new BasicNameValuePair("language", "en")); 

// http post 
try { 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost(PATH); 
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
    HttpResponse response = httpclient.execute(httppost); 
    HttpEntity entity = response.getEntity(); 
    is = entity.getContent(); 

} catch (Exception e) { 
    Log.e("log_tag", "Error in connection " + e.toString()); 
    // results.setText("Error in connection"); 
} 

// convert response to string 
try { 
    BufferedReader reader = new BufferedReader(new InputStreamReader(
      is, "iso-8859-1"), 8); 
    StringBuilder sb = new StringBuilder(); 
    String line = null; 
    while ((line = reader.readLine()) != null) { 
     sb.append(line + "\n"); 
    } 
    is.close(); 
    result = sb.toString(); 
} catch (Exception e) { 
    Log.e("log_tag", "Error converting result " + e.toString()); 

} here 
+0

ArrayList results = new ArrayList (); ? – kami

+0

對不起,這是我自己的類實現打包響應,但既然你不需要它,你可以忽略它 –

+0

感謝您的幫助,我測試了這個,reader.readline()爲null,沒有任何回報,與以前相同的問題碼。 – kami