2016-04-29 62 views
1

我想從httppost請求獲得響應。我得到的網絡響應如200,405,404,但我沒有得到來自服務器的價值。我嘗試了很多,但沒有得到迴應。請幫助...如何獲得HttpPost的響應

我的代碼如下─

私人無效UploadPost(){

 SharedPreferences sharedPreferences1 = getSharedPreferences("DATA", Context.MODE_PRIVATE); 
     String ID = sharedPreferences1.getString("id", ""); 
     @SuppressWarnings("deprecation") 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost(Url.addOffer_url); 

     Log.e("uploadFile", "Source File Path " + picturePath); 
     File sourceFile1 = new File(picturePath); 
     if (!sourceFile1.isFile()) { 
      Log.e("uploadFile", "Source File Does not exist"); 
      imgUploadStatus = "Source File Does not exist"; 
     } 


     try { 
      AndroidMultiPartEntity entity = new AndroidMultiPartEntity(); 
      File sourceFile = new File(picturePath); 
      MultipartEntity entity1 = new MultipartEntity(
        HttpMultipartMode.BROWSER_COMPATIBLE); 
      // Adding file data to http body 


      entity.addPart("retailer_id", new StringBody(ID)); 
      entity.addPart("title", new StringBody(addoffertitle)); 
      entity.addPart("description", new StringBody(addofferdesc)); 
      entity.addPart("keyword", new StringBody(addofferkeyword)); 
      entity.addPart("offer_id", new StringBody(OfferListing_Id)); 
      // entity.addPart("payment_status",new StringBody(paymentStatus)); 
      // if(!picturePath.equals("")) 
      entity.addPart("offer_image", new FileBody(sourceFile)); 
      /* else 
       entity.addPart("old_pic",new StringBody(Image_Path));*/ 
      httppost.setEntity(entity); 

      Log.d("httppost success", "httppost"); 

      //Run a api for net conn check 
      try { 

       String responseString= new String(); 
       HttpResponse response = httpclient.execute(httppost); 
       HttpEntity entity8 = response.getEntity(); 
       if(entity8 !=null){ 
        responseString = EntityUtils.toString(entity8, "UTF-8"); 
        System.out.println("Response body: " + responseString); 
       } 



       statusCode = 200; 

      } catch (FileNotFoundException e) { 
       Log.e("log_tag1", "Error FileNotFoundException new service" + e.toString()); 
       result = "FileNotFoundException"; 
      } catch (SocketTimeoutException e) { 
       Log.e("log_tag2", "SocketTimeoutException new service " + e.toString()); 
       result = "SocketTimeoutException"; 
      } catch (Exception e) { 
       Log.e("log_tag3", "Error converting OtherException new service " + e.toString()); 
       result = "OtherException"; 
      } 


      if (statusCode == 200) { 
       // Server response 
       responseString = "success"; 



       Log.e("complete success", "Response from server: " + responseString); 
      } else if (statusCode == 404) { 
       responseString = "page not found"; 


       Log.e("complete page not found", "Response from server: " + responseString); 

      } else if (statusCode == 405) { 
       responseString = "no net"; 


       Log.e("complete no net", "Response from server: " + responseString); 

      } else { 
       responseString = "other"; 


       Log.e("complete other", "Response from server: " + responseString); 

      } 

     } catch (Exception e) { 
      responseString = e.toString(); 
      responseString = "other"; 
      Log.e("complete", "Response from server: " + responseString); 

     } 

    } 

我想從httppost.i獲得網絡的響應,但我沒有得到迴應來自服務器的價值。我嘗試了很多,但我沒有得到回覆。請幫助...

+0

什麼樣的網絡響應代碼你好嗎?是200,400還是它是什麼? – ishmaelMakitla

+1

嗨,感謝您的回覆。 @ ishmaelMakitla我從服務器獲得200響應,我想獲得json值。 –

+0

'System.out.println(「Response:」+ s);'。這應該是'System.out.println(「Response:」+ s.toString());'。它打印什麼?這不是服務器返回的值,而是文本。由線組成。頁面。可能是一個html頁面或json文本。 – greenapps

回答

0

嘗試使用EntityUtils而不是BufferedReader。舉例來說,像這樣:

String responseString= new String(); 
HttpResponse response = httpclient.execute(httppost); 
HttpEntity entity = response.getEntity(); 
if(entity !=null){ 
    responseString = EntityUtils.toString(entity, "UTF-8"); 
} 

看所選答案here - 它顯示瞭如何獲得響應機構400 HTTP響應。你也可以看看this example。如果您正在使用JSON有效載荷的工作,也許你可能要考慮使用排球 - here are some examples for PUT, POST, and GET requests using Volley

+0

喜@ ishmaelMakitla,無法得到響應而不抽風?實際上需要獲得對服務器響應的響應?按照這個鏈接http://www.androidhive.info/2014/12/android-uploading-camera-image-video-to-server-with-progress-bar/ – Suman

+0

你可以得到沒有使用Volley的反應,我只建議Volley因爲我發現它更易於使用 - 但是請嘗試建議的答案,並讓我知道你是否還沒有得到任何答覆。 – ishmaelMakitla

+0

對不起@ishmaelMakitla我沒有得到任何迴應 –

0

您可以嘗試取而代之的是

InputStream inputStream = httpResponse.getEntity().getContent(); 
InputStreamReader inputStreamReader = new InputStreamReader(inputStream); 
BufferedReader bufferedReader = new BufferedReader(inputStreamReader); 
StringBuilder stringBuilder = new StringBuilder(); 
String bufferedStrChunk = null; 
while((bufferedStrChunk = bufferedReader.readLine()) != null){ 
     stringBuilder.append(bufferedStrChunk); 
} 

HttpEntity entity8 = response.getEntity(); 
       if(entity8 !=null){ 
        responseString = EntityUtils.toString(entity8, "UTF-8"); 
        System.out.println("Response body: " + responseString); 
       }