Posts HttpPost minimal
Post
Cancel

HttpPost minimal

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
        String URL = "http://x.com/sign_up.php";

		ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

		nameValuePairs.add(new BasicNameValuePair("email",email));
		nameValuePairs.add(new BasicNameValuePair("password", pass));

        HttpClient httpClient = new DefaultHttpClient();

        HttpPost post =  new HttpPost(URL); 
        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        try {
    		HttpResponse response = httpClient.execute(post);

    		String responseString = EntityUtils.toString(response.getEntity());
            Log.w("test", responseString);
        } catch (IOException e) {
            e.printStackTrace();
        }

optimized for JSON read

1
2
3
4
5
//get_records.php
$rows = getSet($db,"select * from mytable where OWNER_GUID=?",array($_POST['guid']));

if ($rows!=null)
	echo json_encode($rows);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
	public List<Automotos> getAutomotos(String auth) throws Exception {
        String URL = "http://x.com/get_records.php";

		ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

		nameValuePairs.add(new BasicNameValuePair("guid",auth));
		nameValuePairs.add(new BasicNameValuePair("tbl","automotos"));

        HttpClient httpClient = new DefaultHttpClient();

        HttpPost post =  new HttpPost(URL); 
        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        List<Automotos> servList = new ArrayList<Automotos>();

        try {
    		HttpResponse response = httpClient.execute(post);

    		String responseString = EntityUtils.toString(response.getEntity());

    		JSONTokener tokener = new JSONTokener(responseString);
    		JSONArray jArray = new JSONArray(tokener);

    		for(int i=0; i<jArray.length(); i++){
    			JSONObject json_data = jArray.getJSONObject(i);
    			servList.add(new Automotos(json_data.getString("PLATE"), json_data.getString("DESCRIPTION"), Long.parseLong(json_data.getString("COUNTRY_ID")), json_data.getString("AUTOMOTO_GUID"),  json_data.getString("AUTOMOTO_DATEREC")));
    		}

    		return servList;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
	}

origin - http://www.pipiscrew.com/?p=2081 php-httppost-minimal

This post is licensed under CC BY 4.0 by the author.
Contents

Trending Tags