开发者

make use of php GET and POST in android programming

开发者_开发技巧try {
    HttpClient client = new DefaultHttpClient();  
    String postURL = "http://somepostaddress.com";
    HttpPost post = new HttpPost(postURL);
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("user", "kris"));
        params.add(new BasicNameValuePair("pass", "xyz"));
        UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,HTTP.UTF_8);
        post.setEntity(ent);
        HttpResponse responsePOST = client.execute(post);  
        HttpEntity resEntity = responsePOST.getEntity();  
        if (resEntity != null) {    
            Log.i("RESPONSE",EntityUtils.toString(resEntity));
        }
} catch (Exception e) {
    e.printStackTrace();
}

this is the code that i found in http://www.softwarepassion.com/android-series-get-post-and-multipart-post-requests. afterward i will just only use the simple POST method the variable in the php, right?

example:

$user=$_POST['user']; $pass=$_POST['pass'];

however, when i execute the code, the php showed a 0 only. may i know how to solve it? thanks

p/s: sry for bad english


HttpClient doesn't like to send query strings on URLs for a POST. Though I did find this...

PostMethod.addParameter() and PostMethod.setRequestBody() are mutually exclusive as they both specify the POST entity and only one can be used at a time. If you want to send parameters via the URI query string please use HttpMethod.setQueryString(NameValuePair[]).


Try this method:

    HttpURLConnection connection;
    OutputStreamWriter request = null;

    URL url = null;        
    String parameters = "username=username&password=password";        
    String response = null;

    try
    {
        url = new URL("your login URL");
        connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        connection.setRequestMethod("POST");    

        request = new OutputStreamWriter(connection.getOutputStream());
        request.write(parameters);
        request.flush();
        request.close();

        if(connection.getResponseCode() == HttpURLConnection.HTTP_OK)
        {
            String line = "";

            InputStreamReader isr = new InputStreamReader(connection.getInputStream());
            BufferedReader reader = new BufferedReader(isr);
            StringBuilder sb = new StringBuilder();
            while ((line = reader.readLine()) != null)
            {
                sb.append(line + "\n");
            }
            response = sb.toString();
            isr.close();
            reader.close();
        }           
        else
        {
            // error while connecting to the server. please try afetr some time.
        }
    }
    catch(IOException e)
    {
        //Error
    }


For GET requests - just put it in the parameter line.

For POST requests - do this:

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();  
nameValuePairs.add(new BasicNameValuePair("params1", value1));  
nameValuePairs.add(new BasicNameValuePair("params2", value2));  
...

HttpPost requestMethod = new HttpPost(THE_URL);

 try {
    ((HttpPost)requestMethod).setEntity(new UrlEncodedFormEntity(nameValuePairs));
 } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
 }

 ...
 client.execute(requestMethod);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜