Send JSON object via HttpPost method

by Alok Kulkarni » Tue, 08 Sep 2009 21:19:18 GMT


Sponsored Links
 Any answers ??





--~--~---------~--~----~------------~-------~--~----~



Send JSON object via HttpPost method

by Wayne Wenthin » Wed, 09 Sep 2009 05:39:03 GMT


 A snippet for what I do...
public HttpResponse updateGirl(String url, Pawn girl, int pawnID) {
HttpClient httpclient = new DefaultHttpClient();
HttpPut httpput = new HttpPut(url);

// Execute the request

try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
39);
nameValuePairs.add(new BasicNameValuePair("id", Integer
.toString(pawnID)));
......  Many nameValuePairs later.....


httpput.addHeader("Content-Type",
"application/x-www-form-urlencoded");
httpput.setEntity(new UrlEncodedFormEntity(nameValuePairs,
HTTP.UTF_8));
 HttpResponse response = httpclient.execute(httpput);
return response;

} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;

}

Hope that helps.   I struggled with it for a couple of weeks before I found
a site that had a good source.









-- 
Writing code is one of few things
that teaches me I don't know everything.

--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


Send JSON object via HttpPost method

by Alok Kulkarni » Wed, 09 Sep 2009 16:44:42 GMT


 ey Wayne, thanks a lot..
I have also got a solution parallely which i would like to post

URL url = new URL(serverURL);

// open the conncetion
HttpURLConnection connection =
(HttpURLConnection)url.openConnection();

// Let the run-time system (RTS) know that we want input.
connection.setDoInput(true);
// Let the RTS know that we want to do output
connection.setDoOutput(true);
// No caching, we want the real thing
connection.setUseCaches(false);
// set the content type property
connection.setRequestProperty("Content-type",strContenttype);

// set request method
connection.setRequestMethod("POST");
// create the post body to send
String content = credDevPair.toString();
Log.i("Request ====....... ",content);
DataOutputStream printout = new DataOutputStream (
connection.getOutputStream () );

// send the data
printout.writeBytes(content);
printout.flush();
printout.close();
String output =
convertStreamToString(connection.getInputStream());
Log.i("Response 1....... ",output);
// A Simple JSONObject Creation
JSONObject json=new JSONObject(output);

Log.i("Praeda","<jsonobject>\n"+json.toString()+"\n</jsonobject>");

// A Simple JSONObject Parsing
JSONArray nameArray=json.names();
JSONArray valArray=json.toJSONArray(nameArray);
for(int i=0; i<valArray.length() ;i++)
{

Log.i("Praeda","<jsonname"+i+">\n"+nameArray.getString(i)+"\n</jsonname"+i+">\n"

+"<jsonvalue"+i+">\n"+valArray.getString(i)+"\n</jsonvalue"+i+">");
}
//BufferedReader input = new BufferedReader ( new
InputStreamReader(connection.getInputStream()) );

}catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();

}catch(Exception ex)
{

}

}
private static String convertStreamToString(InputStream is) {
/*
* To convert the InputStream to String we use the
BufferedReader.readLine()
* method. We iterate until the BufferedReader return null which
means
* there's no more data to read. Each line will appended to a
StringBuilder
* and returned as String.
*/
BufferedReader reader = new BufferedReader(new
InputStreamReader(is));
StringBuilder sb = new StringBuilder();

String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}

return sb.toString();
}


On Wed, Sep 9, 2009 at 3:08 AM, Wayn



Send JSON object via HttpPost method

by Wayne Wenthin » Thu, 10 Sep 2009 08:58:06 GMT


 ep that is what I vaguely based mine on.   I recognize the Praeda line.   I
had to modify mine because I was not sending back an array of json objects
but a specific one that I handle based on the ID on the backend with ruby.

On Wed, Sep 9, 2009 at 1:43 AM, Alok Kulkarni <kulsu...@gmail.com> wrote:



--
Writing code is one of few things
that teaches me I don't know everything.

--~--~---------~--~----~------------~-------~--~----~



Send JSON object via HttpPost method

by Alok Kulkarni » Thu, 10 Sep 2009 13:28:31 GMT


 k.. ya i had taken that code part from the forum itself. :)

On Thu, Sep 10, 2009 at 6:27 AM, Wayne Wenthin <wa...@fuligin.com> wrote:


--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. ProgressDialog Problem with Rotation

I have a managed ProgressDialog that show the status as tasks are
moving along.  The Activity.show is kicked off by an AsyncTask
progressUpdates and the final Activity.closeDialog is called through
the AsyncTask as well.  Everything works beautifully as long as the
screen is not rotated...

What am I doing wrong? I thought having the Activity manage my dialog
would solve this for me... Here is the code I'm playing with:


public class Enter extends Activity {
        ProgressDialog progressDialog;
        private final static int PROGRESS_DIALOG_ID = 1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button button = (Button)findViewById(R.id.progressbar_button);

        button.setOnClickListener(new View.OnClickListener(){

                        @SuppressWarnings("unchecked")
                        public void onClick(View v) {

                                Enter.this.showDialog(PROGRESS_DIALOG_ID);
                                new AsyncTask(){

                                        @Override
                                        protected Object 
doInBackground(Object... params) {
                                                progressDialog.setMax(30);
                                                for (int a = 0; a < 30; a++){
                                                        publishProgress(new 
Integer((a+1)));
                                                        try {
                                                                
Thread.sleep(1000);
                                                                Log.d("Enter", 
"Click: " + a);
                                                        } catch 
(InterruptedException e) {
                                                                
e.printStackTrace();
                                                        }
                                                }
                                                return null;
                                        }

                                        @Override
                                        protected void 
onProgressUpdate(Object... values) {
                                                super.onProgressUpdate(values);
                                                Integer progress = 
(Integer)values[0];
                                                
progressDialog.setProgress(progress.intValue());
                                        }

                                        @Override
                                        protected void onPostExecute(Object 
result) {
                                                super.onPostExecute(result);
                                                
Enter.this.dismissDialog(PROGRESS_DIALOG_ID);
                                        }
                                }.execute((Object[]) null);
                        }
        });
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        Dialog dialog = null;
        if (id == PROGRESS_DIALOG_ID){
            progressDialog = new ProgressDialog(this);
 
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressDialog.setTitle("Pleae Wait");
            progressDialog.setMessage("Running a long process...");
            progressDialog.setCancelable(true);
            dialog = progressDialog;
        }
        return dialog;
    }

Thanks!

-Aaron

-- 

2. Strange ListView divider behaviour

Hi,
can anyone help me out with a problem I've run into?

I have been trying to create a ListView Activity with an ImageView in
every row. I've used this tutorial:
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List8.html

The issue is that there are always some dividers missing whenever I
run the example. The dividers are disappearing/appearing and
flickering as i scroll the list (tested in emulator and HTC Tattoo).

Strange is that the same example downloaded from the market (API Demos
app) behaves correctly.

Here is a screenshot of how it looks:
http://yfrog.com/4cnokp

Thanks
Andy

-- 

3. Go back to previous activity

4. How to Debug

5. Need Assistance !!!

6. Having a back button functioning like the emulators

7. CLosed: OOT: WTA AXIS bocor?