2016-09-07 144 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代码此方法。