0

in my app i need to post data to an url to register a new user. Here is the url

http://myurl.com/user.php? email=[EMAIL]&username=[USERNAME]&password[PASS]&img_url=[IMG]

If I do that correctly I should get this message:

{"success":true,"error":null} 
or if not {"success":false,"error":"parameters"}

Can somebody guide me through this and tell me how can I do it.

2 Answers 2

3

first :
you need to perform all network tasks in an Async thread using:

public class PostData extends AsyncTask<String, Void, String>{
{
        @Override
    protected String doInBackground(String... params) {
    //put all your network code here
}

Second:
create your http request: i am assuming email, username and IMG as variables over here.

    String server ="http://myurl.com/user.php? email=[" + EMAIL + "]&username=[" + USERNAME + "]&password[" + PASS + "]&img_url=["+IMG + "]";

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(server);

            //httppost.setHeader("Accept", "application/json");
            httppost.setHeader("Accept", "application/x-www-form-urlencoded");
            //httppost.setHeader("Content-type", "application/json");
            httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");

third:     
// Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
            nameValuePairs.add(new BasicNameValuePair("JSONdata", Object));     
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));

            try {
                HttpResponse response =httpclient.execute(httppost);

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

Now simple query your response handler i.e. response in this case.

Don't forget to add INTERNET permission in your androidManifest.xml

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

0

Use a HTTP client class, and format your URL via a specific URI constructor. Create a HTTP post, optionally set the entity, headers, etc, execute the post via the client, receive a HTTP response, pull the entity out of the response and process it.

EDIT for example:

HttpClient httpclient = new DefaultHttpClient();
URI uri = new URI("http",  
        "www.google.com",  // connecting to  IP
        "subpath", // and the "path" of what we want
        "a=5&b=6", // query 
        null); // no fragment
HttpPost httppost = new HttpPost(uri.toASCIIString);
// have a body ?
// post.setEntity(new StringEntity(JSONObj.toString()));
// post.setHeader("Content-type", "application/json");
HttpResponse response = httpClient.execute(post);
int statusCode = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
Reader r = new  InputStreamReader(entity.getContent());
// Do something with the data in the reader.

1 Comment

Can you give me an example how to do it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.