如果检查帖子方法如果在android中?

时间:2014-03-28 13:35:45

标签: android http-post

我想尝试使用post方法登录网站...如何检查我是否登录!!?

public void postData() {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("https://svuonline.org/isis/index.php");
try {if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("user_name", user.getText().toString()));
        nameValuePairs.add(new BasicNameValuePair("user_pass",password.getText().toString()));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
    } catch (ClientProtocolException e) { } 
catch (IOException e) { }
catch (Exception e) { }
} 

2 个答案:

答案 0 :(得分:0)

你快到了!您已经在HttpResponse响应中获得响应,您只需检查呼叫的状态代码即可。

HttpResponse response = httpclient.execute(httppost);
int responseCode = response.getStatusLine().getStatusCode();
如果一切正常,

responseCode应该等于HttpStatus.SC_OK。您还可以获得响应的正文:

ResponseHandler<String> mHandler = new BasicResponseHandler();
String body = mHandler.handleResponse(response);

希望这有帮助!

答案 1 :(得分:0)

在这里,您可以找到我编码的类,以使您的服务器响应为字符串:Java : a WebService asking embeded in a AsyncTask

提供了如何使用,只需替换

WebServiceAsyncTask.GET 

通过

WebServiceAsyncTask.POST

并指定您的参数。

然后你必须检查服务器响应。


看看paresh-mayani评论,图书馆听起来很棒。