Click here to Skip to main content
15,911,030 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello friends,

here i want to pass the values to json object, but here im getting an error,please rectify it. which iam going to pass the values to services.

below code


Java
public class MainActivity extends ActionBarActivity {


    EditText e1, e2;

    Button b1;
    HttpClient client;
    String url = "http://localhost:52455/bloodservice/Service.svc";
    JSONObject json;
    final Context context = this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b1 = (Button) findViewById(R.id.button);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)  {


                e1 = (EditText) findViewById(R.id.editText);
                e2 = (EditText) findViewById(R.id.editText2);
                String t1 = e1.getText().toString();
                String t2 = e2.getText().toString();
                
                HttpClient client = new DefaultHttpClient();
                HttpResponse response;
                JSONObject json = new JSONObject();

                HttpPost post = new HttpPost(url);
                json.put("userName",t1); //HERE IM GETTING ERROR UNHANDLED EXCEPTION org.json,JSONException
                json.put("userPassword", t2);//HERE IM GETTING ERROR UNHANDLED EXCEPTION org.json,JSONException
                StringEntity se=null;
                try {
                    se = new StringEntity(json.toString());
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                se.setContentType("application/json;charset=UTF-8");
                se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
                post.setEntity(se);
                response = Client.execute(post);//HERE IM GETTING ERROR UNHANDLED EXCEPTION org.apache.http.client.ClientProtocolException.
Posted
Updated 18-Dec-14 22:18pm
v2

Try add method..
C#
json.Add("userName",t1);
json.Add("userPassword", t2);
 
Share this answer
 
Comments
Mr.VJ 19-Dec-14 4:32am    
Thanks for your reply,
Its not working ,
showing
Cannot resolve method ' Add(java.lang.String,java.lang.String)'.
This one rectified by putting try/catch.

C#
try {
                    json.put("userName", t1);
                    json.put("userPassword", t2);
                }catch (JSONException e) {
                    e.printStackTrace();
                }




but this one getting same error.
Java
<pre lang="java">response = Client.execute(post);//HERE IM GETTING ERROR UNHANDLED EXCEPTION org.apache.http.client.ClientProtocolException.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900