0

I just want to pass the json raw data to httpclient . Basically what i want is my http client consumes and produces application/json.

How can I pass the the following values.

Basic Auth:
username: *******
password: *******
POST URL: http://casetestbu.com/c****butest/services/photos/upload
Headers Content-Type:application/json
Raw Data:
{"eventId":4,"eventDescription":"grtrt","fileName":"test5.jpg","fileType":"jpg","imageData":"/9j/4AAQSkZJRgABAgAAAQABAAD/gAEKgD/4gIcSUN"};

Could you please help me with some code snips.

Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
Gowtham
  • 47
  • 1
  • 1
  • 8
  • explore the link http://stackoverflow.com/questions/8267928/android-rest-client-sample – RQube Apr 15 '14 at 07:21
  • Never use AsynTask to perform network request or whatever that need to be persisted. Async Task are strongly tied to your activity and if the user change the orientation of the screen since the App is re created the AsyncTask will be stopped. AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent pacakge such as Executor, ThreadPoolExecutor and FutureTask. – RQube Apr 15 '14 at 07:23
  • Sir can you explain an good alternative to asynktask.? i am new to android and learning. can you give me links related to executer threadpool executer and future task – Tushar Narang Apr 15 '14 at 07:25

1 Answers1

0

just declare the variables above

public void onClick(View view) {

        switch(view.getId())
        {




            case R.id.btnSingIn:





                  JSONObject login = new JSONObject();
                try
                {
                login.put("Username", editText1.getText().toString());
                login.put("Password", editText2.getText().toString());

                JSONObject finaldata = new JSONObject();
                finaldata.put("LoginRequest", login);
//               Toast.makeText(getBaseContext(), finaldata.toString(), Toast.LENGTH_LONG).show();


                  final ConnectToServer connect = new ConnectToServer();
                connect.extConnectToServer(MainActivity.this,new ConnectToServer.Callback() 

                {
                    public void callFinished(String result)
                    {
                        //Toast.makeText(getBaseContext(), result, Toast.LENGTH_LONG).show();

                        JSONObject resp = null;
                        try 
                        {
                            resp = new JSONObject(result);
                            //Toast.makeText(getBaseContext(), result, Toast.LENGTH_LONG).show();
                            JSONObject Login1Result = resp.getJSONObject("LoginResult");
                            String strMessage = Login1Result.getString("EmployeeID");

                        JSONObject status = Login1Result.getJSONObject("status");
//                      Toast.makeText(getBaseContext(), status.getString("message"), Toast.LENGTH_LONG).show();
                        if (status.getString("message").equalsIgnoreCase("OK"))
                        {
//                            Toast.makeText(getBaseContext(), strMessage, Toast.LENGTH_LONG).show();

                           Intent i = new Intent(getApplicationContext(), Tabs.class);
                           i.putExtra("new_variable_name",strMessage);
                           startActivity(i);  

                            }
                            else
                            {
                            Toast.makeText(getBaseContext(),  status.getString("message"), Toast.LENGTH_LONG).show();


                            }


                        }

                        catch (final JSONException e)
                        {

                        }

                    }
                            }, "www.Your url whatever it is.com", finaldata,
                        "POST");
                    connect.execute(finaldata);

                }
                 catch (Exception e) {
                    Log.d("InputStream", e.getLocalizedMessage());
               }

            break;
        }
        }
        }
    }
Tushar Narang
  • 1,997
  • 3
  • 21
  • 49
  • dont forget to add aynctask class to your project and declare the editText 's and their button in the onCreate. – Tushar Narang Apr 15 '14 at 07:14
  • for further information use [this](http://hmkcode.com/android-send-json-data-to-server/) link for asynktask class and getting ur basics right – Tushar Narang Apr 15 '14 at 07:17