Search code examples
androidhttpclienthttprequesthttpresponsehttpcontext

http request and http response in android?


I am working on with:

org.apache.http.HttpResponse;
org.apache.http.client.HttpClient;
org.apache.http.client.methods.HttpGet;
org.apache.http.impl.client.DefaultHttpClient;

i am using jsp for writing server side code.

i am unaware of the procedure of sending some value to server from my emulator. and get a value o a string from the server response.

I mean to say... what are the methods to be used? How to use them.?


Solution

  • Below is a code i implemented for sending username and password from android to server side JSP page. The response from the server is a json object which is then processed.The code is as given below.

    import java.util.ArrayList;
    import java.util.List;
    
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.util.EntityUtils;
    import org.json.JSONException;
    import org.json.JSONObject;
    
    import android.app.Activity;
    import android.app.ProgressDialog;
    import android.content.Intent;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    
    
    public class Login_Menu extends Activity {
    
    EditText usname;
    EditText pass;
    TextView tv;
    HttpClient client;
    HttpPost post;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login_lay);
     tv=(TextView) findViewById(R.id.login_stat_tv);
     usname=(EditText)findViewById(R.id.uname);
     pass=(EditText)findViewById(R.id.pass);
    Button login=(Button)findViewById(R.id.login_but);
    Button cancel=(Button)findViewById(R.id.cancel_but);
    
    client = new DefaultHttpClient();
    String url="http://10.0.2.2:7001/proj/login.jsp";
    post = new HttpPost(url);
    login.setOnClickListener(new View.OnClickListener() {
    
        public void onClick(View arg0) {
            new login().execute("");
        }
    });
    
    cancel.setOnClickListener(new View.OnClickListener() {
    
        public void onClick(View v) {
            usname.getText().clear();
            pass.getText().clear();
        }
    });
    
    }
    
    
    
    
    private class login extends AsyncTask<String, Void, JSONObject>{
    
    ProgressDialog dialog = ProgressDialog.show(Login_Menu.this, "", "Authenticating, Please wait...");
    
    @Override
    protected JSONObject doInBackground(String... params) {
        Log.i("thread", "Doing Something...");
       //authentication operation
    try{
    
        List<NameValuePair> pairs = new ArrayList<NameValuePair>();   
        pairs.add(new BasicNameValuePair("username",usname.getText().toString()));   
        pairs.add(new BasicNameValuePair("password",pass.getText().toString()));   
        post.setEntity(new UrlEncodedFormEntity(pairs));   
        HttpResponse response = client.execute(post);
        int status=response.getStatusLine().getStatusCode();
    
        if(status == 200)
        {
            HttpEntity e=response.getEntity();
            String data=EntityUtils.toString(e);
            JSONObject last=new JSONObject(data);
            return last;
    
        }
    
    }
    
      catch(Exception e)
    {
        e.printStackTrace();   
    
    }
    
        return null;
    }
    
    protected void onPreExecute(){
        //dialog.dismiss();
        Log.i("thread", "Started...");
        dialog.show();
    }
    protected void onPostExecute(JSONObject result){
        Log.i("thread", "Done...");
        String status;
        String name;
        try {
            status= result.getString("status");
            name=result.getString("uname");
    
           if(dialog!=null)
           {
             dialog.dismiss();
           }
           else{}
    
         if(status.equalsIgnoreCase("yes"))
              {
            tv.setText("Login Successful...");
    
            Bundle newbundle=new Bundle();
            newbundle.putString("uname",name);
    
            Intent myIntent=new Intent(Login_Menu.this,Instruction.class);
            myIntent.putExtras(newbundle);
    
            startActivity(myIntent);
    
            }
          else{
    
                tv.setText("No User Found, please try again!");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
       }
    
      }
    
     }