2016-09-07 146 views
-1
public static String[] Webcall(String emailID) { 
     try { 
      URL url = new URL(AppConfig.URL + emailID); 
      HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
      conn.setRequestMethod("POST"); 
      conn.setRequestProperty("Accept", "application/json"); 
      conn.setRequestProperty("Authorization", "application/json"); 
      conn.setRequestProperty("userEmailId", emailID); 
      if (conn.getResponseCode() != 200) { 
       throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode()); 
      } 

      BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); 

      String output; 
      System.out.println("Output from Server .... \n"); 
      while ((output = br.readLine()) != null) { 
       System.out.println(output); 
      } 

      conn.disconnect(); 

      org.json.JSONObject _jsonObject = new org.json.JSONObject(output); 
      org.json.JSONArray _jArray = _jsonObject.getJSONArray("manager"); 
      String[] str = new String[_jArray.length()]; 

     } catch (MalformedURLException e) { 

      e.printStackTrace(); 

     } catch (IOException e) { 

      e.printStackTrace(); 

     } 
     return null; 

    } 

這是我的代碼我試圖調用web服務並獲取數據。如何使用post方法在java中調用web服務

但是,當我打的Web服務,然後我得到異常

以下故障:HTTP錯誤代碼:404

請建議我哪裏做錯了,儘量給解決這一點。

+0

對不起,你不知道404是什麼意思? –

+0

爲什麼這個錯誤即將到來你可以告訴 –

+0

谷歌404 http –

回答

0

的第一件事是檢查URL是它在電腦上工作使用郵遞員或RESTClient實現,如果它工作正常,然後試着用下面的代碼後,該代碼是使用HttpPost可以使用改裝張貼JSON格式數據lib就像Milad所建議的那樣。

public static String POST(String url, String email) 
{ 
     InputStream inputStream = null; 
     String result = ""; 
     try { 
      // 1. create HttpClient 
      HttpClient httpclient = new DefaultHttpClient(); 

      // 2. make POST request to the given URL 
      HttpPost httpPost = new HttpPost(url); 
      String json = ""; 
      // 3. build jsonObject 
      JSONObject jsonObject = new JSONObject(); 
      jsonObject.accumulate("email", email); 
      // 4. convert JSONObject to JSON to String 
      json = jsonObject.toString(); 
      // 5. set json to StringEntity 
      StringEntity se = new StringEntity(json); 

      // 6. set httpPost Entity 
      httpPost.setEntity(se); 

      // 7. Set some headers to inform server about the type of the content 
      httpPost.setHeader("Accept", "application/json"); 
      httpPost.setHeader("Content-type", "application/json"); 

      // 8. Execute POST request to the given URL 
      HttpResponse httpResponse = httpclient.execute(httpPost); 

      // 9. receive response as inputStream 
      inputStream = httpResponse.getEntity().getContent(); 

      // 10. convert inputstream to string 
      if(inputStream != null) 
       result = convertInputStreamToString(inputStream); 
      else 
       result = "Did not work!"; 

     } catch (Exception e) { 
      Log.d("InputStream", e.getLocalizedMessage()); 
     } 

     // 11. return result 
     return result; 
    } 



private static String convertInputStreamToString(InputStream inputStream) throws IOException{ 
     BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); 
     String line = ""; 
     String result = ""; 
     while((line = bufferedReader.readLine()) != null) 
      result += line; 

     inputStream.close(); 
     return result; 

    } 
0

我想你應該嘗試重新檢查你發送請求的URL。 請遵循輸出錯誤,代碼404表示URL已損壞或無效鏈接。

0

您可以通過使用jersy客戶端做同樣的jersy core.Here是代碼片段

private static void generateXML(String xmlName, String requestXML, String url) 
    { 
    try 
    { 
     Client client = Client.create(); 
     WebResource webResource = client 
     .resource(url); 
     ClientResponse response = (ClientResponse)webResource.accept(new String[] { "application/xml" }).post(ClientResponse.class, requestXML); 
     if (response.getStatus() != 200) { 
     throw new RuntimeException("Failed : HTTP error code : " + response.getStatus()); 
     } 
     String output = (String)response.getEntity(String.class); 
     PrintWriter writer = new PrintWriter(xmlName, "UTF-8"); 
     writer.println(output); 
     writer.close(); 
    } 
    catch (Exception e) { 
     try { 
     throw new CustomException("Rest-Client May Be Not Working From Your System"); 
     } catch (CustomException e1) { 
     System.exit(1); 
     } 
    } 
    } 

呼叫從varibales代碼此方法。