2015-10-05 150 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(); 
     } 

    } 
} 
相关问题