我尝试从POSTMAN API工作正常,但不是从我的Java代码?

问题描述:

请我已经经历了从计算器所有的问题了,但这些并不适用于我的问题。我尝试从POSTMAN API工作正常,但不是从我的Java代码?

enter image description here 请看图像请求从POSTMAN工作正常。

但是当我从android代码尝试它不工作。 我的示例Android代码在这里。

@Override 
     protected String doInBackground(Void... params) { 
      HttpURLConnection urlConnection = null; 
      BufferedReader reader = null; 

      // Will contain the raw JSON response as a string. 
      String forecastJsonStr = null; 

      try { 
       URL url = new URL("sample url"); 

       String postData = "key1=valu1&key2=valu2"; 

       HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
       conn.setReadTimeout(55000 /* milliseconds */); 
       conn.setConnectTimeout(55000 /* milliseconds */); 
       conn.setRequestMethod("POST"); 
       conn.setDoInput(true); 
       conn.setDoOutput(true); 
       OutputStream os = conn.getOutputStream(); 
       BufferedWriter writer = new BufferedWriter(
         new OutputStreamWriter(os)); 
       writer.write(postData); 

       writer.flush(); 
       writer.close(); 
       os.close(); 

       int responseCode=conn.getResponseCode(); 

       if (responseCode == HttpsURLConnection.HTTP_OK) { 

        BufferedReader in=new BufferedReader(
          new InputStreamReader(
            conn.getInputStream())); 
        StringBuffer sb = new StringBuffer(""); 
        String line=""; 

        while((line = in.readLine()) != null) { 
         Log.e("response ",line); 
         sb.append(line); 
         break; 
        } 

        in.close(); 
        return sb.toString(); 

       } 
       else { 
        return new String("false : "+responseCode); 
       } 
      } catch (Exception e) { 
       Log.e("PlaceholderFragment", "Error ", e); 
       // If the code didn't successfully get the weather data, there's no point in attemping 
       // to parse it. 
       return null; 
      } finally { 
       if (urlConnection != null) { 
        urlConnection.disconnect(); 
       } 
       if (reader != null) { 
        try { 
         reader.close(); 
        } catch (final IOException e) { 

        } 
       } 
      } 
     } 

上面的java代码返回405错误代码。我也试过从OkHttp也。 请帮忙。

把一个“/”在URL的末尾导致重定向发生,因为您的服务器喜欢在“/”结尾的网址。 POST是由服务器重定向到的网址完全支持,但根据您的setRedirecting()调用(卷曲做同样的事情用-L开关)的修复方法是把当它表现在客户端执行GET请求在URL末尾加一个'/',或者自己从响应中获取位置标题,然后手动启动另一个POST请求。

这可以在Wireshark的被观察到。您可以通过尝试使用浏览器对URL执行GET请求来测试理论,其中附有斜线。这将导致浏览器获得405下面是Android上的固定代码,该代码使用附加的“/”的简单修复的URL(未投入生产):

更从here.

阅读不要让我知道如果这有助于:)

此外,尝试使用这段代码:

public void postData() { 
// Create a new HttpClient and Post Header 
HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost("https://your URL"); 

try { 
    // Add your data 
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
    nameValuePairs.add(new BasicNameValuePair("id", "12345")); 
    nameValuePairs.add(new BasicNameValuePair("stringdata", "Hi")); 
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

    // Execute HTTP Post Request 
    HttpResponse response = httpclient.execute(httppost); 

} catch (ClientProtocolException e) { 
    // TODO Auto-generated catch block 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
} 
} 
+1

给出的数据应该连同URL发送给我把这里的问题,但不适合我 –

+1

我的工作也用邮差生成的Java代码 –

+0

你尝试使用不同的URL尝试过? – Han

你的API调用的GET参数reciveving数据。所以下面

  String postData = Uri.encode("key1=valu1&key2=valu2"); 
      URL url = new URL("sample url"+"?+postData);