2016-04-12 51 views
-1

Android URLConnection失敗所以我試圖發佈數據到服務器,我得到了400代碼。我知道這意味着它是一個糟糕的請求,並且是由於我認爲的格式不正確。 所以這就是我被告知要形成JSON當我嘗試發佈

curl -i -u admin:admin -H "Content-Type: application/json" -X POST –d '{"customer_id":1, "device_id":"1234", "notification_id":"NOTIFICATIONID123456", "operating_system_type":"IOS"}' localhost:8090/api/1.0/notificationInformation

所以現在我都不如果服務器有問題,或者我的代碼是問題知道。我之前遇到過有關Android HttpUrlConnection的問題。

因此,這裏是我的HTTP處理程序類:

public class MyHttpPost extends AsyncTask<String, Void, String> { 



private String username; 
private String password; 

private String json = "{\"customer_id\": \"000234\" , \"device_id\":\"1234443\", \"notification_id\":\"fLqDFPgBHcs:APA91bHHIB7kyTRqR18pK78k81AbV211jdhIyNlWK-CmejFKIK6FZJgx4R-7uzyfYLTqi_jhqclks07nkkQFnORXOU28wJ5qC3GIIY_WPaNxKCxIUTltMaWihPGcvaeOgyW7669M3K1n\", \"operating_system_type\":\"Android\"}"; 
///api/1.0/notificationInformation 

public MyHttpPost(){} 
public MyHttpPost(String username, String password) 
{ 
    this.username = username; 
    this.password = password; 
} 
@Override 
protected String doInBackground(String... params) { 
    String url = params[0]; 
    postHttp(url); 
    return null; 
} 

    public void postHttp(String myUrl) 
    { 


    Log.d("Button ", "Pushed"); 
    try { 

     URL url = new URL(myUrl); 
     if (username!=null) 
     { 
      Authenticator.setDefault(new Authenticator() { 
       @Override 
       protected PasswordAuthentication getPasswordAuthentication() { 
        return new PasswordAuthentication(username, password.toCharArray()); 
       } 
      }); 
     } 

     HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 

     // Allow Outputs (sending) 
     connection.setDoOutput(true); 
     Log.d("Allows input: ", Boolean.toString(connection.getDoInput())); 
     connection.setUseCaches(false); 


     // Enable POST method 
     connection.setRequestMethod("POST"); 
     connection.setRequestProperty("Content-Type", "application/json"); 
     connection.setRequestProperty("charset", "utf-8"); 
     DataOutputStream printout = new DataOutputStream(connection.getOutputStream()); 
     //printout.write(json.toString().getBytes("UTF8")); 
     printout.write(json.getBytes("UTF8")); 



     printout.flush(); 
     printout.close(); 
     int statuscode = connection.getResponseCode(); 
     Log.d("Response code: " , Integer.toString(statuscode)); 


    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (ProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    } 

    } 

任何幫助將不勝感激。

+0

使用凌空網絡請求 –

+0

老實說,我只是想東西了這裏,所以我可以學到 – MNM

回答

1

嘗試這樣

URL url = new URL(url); 
        HttpURLConnection conn = (HttpURLConnection)url.openConnection(); 
        conn.setRequestMethod("POST"); 
        conn.setRequestProperty("Content-Type", "application/json"); 
        conn.setDoOutput(true); 
       JSONObject postDataParams = new JSONObject(); 
        try { 
         postDataParams.put("username","myname"); 
         postDataParams.put("password", "mypwd"); 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 

        PrintWriter out = new PrintWriter(conn.getOutputStream()); 
        out.print(postDataParams); 
        out.close(); 

        int responseCode=conn.getResponseCode(); 
+0

我仍然有我的400層的問題,所以我雙重檢查我的JSON字符串以驗證它是正確的。我認爲這是服務器,我必須仔細檢查關鍵值 – MNM

+1

chek這是http://stackoverflow.com/questions/19671317/400-bad-request-http-error-code-meaning – Rgv

+0

是的,我看到了。這就是我瞭解到400意味着什麼的地方:) – MNM

相關問題