1

I am using get method which works perfectly.I would like to know how to edit the following code to accept post method ?? I am would like to use the same code as below for post method(with necessary changes).

I would appreciate any help.

Thanks in Advance.

JsonParsingExample

public class JsonParsingExample extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        JSONObject json=JSONfunctions.getJSONfromURL("http://example.com/app/login.php?username=a&password=a");
        Log.w("json: ", json.toString());

       }
       }

Jsonfunctions.java

public class JSONfunctions {

    public static JSONObject getJSONfromURL(String url){
        InputStream is = null;
        String result = "";
        JSONObject jArray = null;

        //http post
        try{
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(url);
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();

        }catch(Exception e){
                Log.e("log_tag", "Error in http connection "+e.toString());
        }

      //convert response to string
        try{
                BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                }
                is.close();
                result=sb.toString();
        }catch(Exception e){
                Log.w("log_tag", "Error converting result "+e.toString());
        }

        try{

            jArray = new JSONObject(result);            
        }catch(JSONException e){
                Log.w("log_tag", "Error parsing data "+e.toString());
        }

        return jArray;
    }
}

1 Answer 1

2

Try this:

public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");

try {
    // Add your data
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair("id", "12345"));
    nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    // Execute HTTP Post Request
    HttpResponse response = httpclient.execute(httppost);

} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
} catch (IOException e) {
    // TODO Auto-generated catch block
}
} 
Sign up to request clarification or add additional context in comments.

7 Comments

Thank You Sir for looking into the question.Should I use jsonfunctions.class??
If your goal is simply to send an HTTP POST request then no, you don't need the json class. You can insert the above code right into your onCreate method.
So what is jsonfunctions.java doing in get method or in other words why do I need it in Get Method.Please help me clear this query it would help me a great deal.And also what if the post method takes more time than expected ??
please do let me know the query above ,I am accepting the answer for the question I had asked.Thanks You Sir.I will try it out and get back to you for any queries.
The JSONFunction is a class. The function inside it is JSONObject which just accepts a URL parameter. If you want your POST request to exist inside another class and be able to pass the URL to it that's how you would do it. Sometimes that's nice for cleaner code, but it's not necessary.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.