2015-10-05 78 views
-2

我是Android的新手。我也在StackOverflow中經歷了許多類似的問題,但無法獲得主旨。POST請求與HttpURLConnetion並從服務器讀取響應 - Android

我正在向使用用戶名和密碼的服務器發送發佈請求。我的服務器只是檢查憑據是否爲真,併發送「成功」或「失敗」響應。我只需要捕捉服務器的響應 - 就這些了。以下是我迄今爲止遇到的情況。

public String CheckUserAuthentication(String name, String pass) { 
    try { 
     URL url = new URL("http://sample.com?username=" + name + "&password=" + pass); 
     try { 

      HttpURLConnection con = (HttpURLConnection) url.openConnection(); 
      con.setRequestMethod("POST"); 
      con.setDoOutput(true); 

     } catch (IOException e) { 
     } 

    } catch (MalformedURLException e) { 

    } 
    return Response; 
} 

如果有人建議我轉發請求並從服務器接收回應,我會很高興。

+0

用戶名和密碼,看起來像GET請求,不POST,請搜索更多的示例代碼可用於SO :) – BNK

回答

0

我附上了一個你需要什麼的例子的鏈接。它不是100%的android,但代碼應該在Android平臺上工作(是Java,杜)。

只需複製,粘貼並進行必要的修改即可。

乾杯, 鮑勃

http://www.mkyong.com/java/how-to-send-http-request-getpost-in-java/

+0

嗨,謝謝你的鏈接。代碼看起來簡單易懂。但我遇到了一個問題。只要我引入DataOutputStream wr = new DataOutputStream(con.getOutputStream());我的應用程序失敗。我一直無法理解這個問題。沒有未捕獲的異常。有什麼建議嗎? – user3303274

0

嘗試此

HttpURLConnection con = (HttpURLConnection) url.openConnection(); 
    con.setRequestMethod("POST"); 
    con.setDoOutput(true); 

    DataOutputStream outputStream = new DataOutputStream(con.getOutputStream()); 

    String jsonParamsString = "{\"key\":\"value\"}"; 
    outputStream.writeBytes("request=" + jsonParamsString); 
    outputStream.flush(); 
    outputStream.close(); 

    // get response 
    InputStream responseStream = new BufferedInputStream(con.getInputStream()); 
    BufferedReader responseStreamReader = new BufferedReader(new InputStreamReader(responseStream)); 
    String line = ""; 
    StringBuilder stringBuilder = new StringBuilder(); 
    while ((line = responseStreamReader.readLine()) != null) { 
     stringBuilder.append(line); 
    } 
    responseStreamReader.close(); 

    String response = stringBuilder.toString(); 
    JSONObject jsonResponse = new JSONObject(response); 

結果是響應然後將其轉化到JSON對象,則可以解析它。

0

使用的AsyncTask喜歡這個

new MyTaskLogin().execute(Constant.LOGIN_URL + "&email=" + strUserEmail 
        + "&password=" + strUserPassword); 

AsynchTask代碼: - 在URL

private class MyTaskLogin extends AsyncTask<String, Void, String> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     bar.setVisibility(View.VISIBLE); 
     layout.setVisibility(View.INVISIBLE); 
    } 

    @Override 
    protected String doInBackground(String... params) { 
     return JsonUtils.getJSONString(params[0]); 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     super.onPostExecute(result); 

     bar.setVisibility(View.GONE); 

     if (null == result || result.length() == 0) { 
      setSweetDialog(SweetAlertDialog.ERROR_TYPE, 
        getString(R.string.conn_msg1), 
        getString(R.string.nodata)); 

     } else { 

      try { 
       JSONObject mainJson = new JSONObject(result); 
       JSONArray jsonArray = mainJson 
         .getJSONArray(Constant.USER_LOGIN_ARRAY); 
       JSONObject objJson = null; 
       for (int i = 0; i < jsonArray.length(); i++) { 
        objJson = jsonArray.getJSONObject(i); 
        if (objJson.has(Constant.USER_LOGIN_MSG)) { 
         strMessage = objJson 
           .getString(Constant.USER_LOGIN_MSG); 
         Constant.GET_SUCCESS_MSG = objJson 
           .getInt(Constant.USER_LOGIN_SUCESS); 
        } else { 
         Constant.GET_SUCCESS_MSG = objJson 
           .getInt(Constant.USER_LOGIN_SUCESS); 
         strUserEmail = objJson 
           .getString("string_name1"); 
         strUserName = objJson 
           .getString("string_name2"); 

        } 
       } 

      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

      setResult(); 
     } 

    } 
} 
相關問題