Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How send http request in android app to access REST API

Tags:

android

Can any one solve my problem. I want to send a http request in android to access REST API(PHP)..

Thanks

like image 366
sandy Avatar asked Apr 24 '11 08:04

sandy


2 Answers

http://breaking-catch22.com/?p=12

public class AndroidApp extends Activity {  

    String URL = "http://the/url/here";  
    String result = "";  
    String deviceId = "xxxxx" ;  
    final String tag = "Your Logcat tag: ";  

    /** Called when the activity is first created. */  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  

        final EditText txtSearch = (EditText)findViewById(R.id.txtSearch);  
        txtSearch.setOnClickListener(new EditText.OnClickListener(){  
            public void onClick(View v){txtSearch.setText("");}  
        });  

        final Button btnSearch = (Button)findViewById(R.id.btnSearch);  
        btnSearch.setOnClickListener(new Button.OnClickListener(){  
            public void onClick(View v) {  
                String query = txtSearch.getText().toString();  
                callWebService(query);  

            }  
        });  

    } // end onCreate()  

    public void callWebService(String q){  
        HttpClient httpclient = new DefaultHttpClient();  
        HttpGet request = new HttpGet(URL + q);  
        request.addHeader("deviceId", deviceId);  
        ResponseHandler<string> handler = new BasicResponseHandler();  
        try {  
            result = httpclient.execute(request, handler);  
        } catch (ClientProtocolException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        httpclient.getConnectionManager().shutdown();  
        Log.i(tag, result);  
    } // end callWebService()  
} 
like image 71
Waza_Be Avatar answered Nov 11 '22 10:11

Waza_Be


It basically depends on what you need, but assuming a simply POST request with a JSON body it'll look something like this (I'd suggest to use Apache HTTP Library).

HttpPost mRequest = new HttpPost(<your url>);    

DefaultHttpClient client = new DefaultHttpClient();
//In case you need cookies, you can store them with PersistenCookieStorage
client.setCookieStore(Application.cookieStore);

try {
    HttpResponse response = client.execute(mRequest);

    InputStream source = response.getEntity().getContent();
    Reader reader = new InputStreamReader(source);

    //GSON is one of the best alternatives for JSON parsing
    Gson gson = new Gson();

    User user = gson.fromJson(reader, User.class);

    //At this point you can do whatever you need with your parsed object.

} catch (IOException e) {
    mRequest.abort();
}

Lastly, I'd encourage you to run this code in any kind of background thread (executor, thread, asynctask, etc)

like image 29
Jose L Ugia Avatar answered Nov 11 '22 10:11

Jose L Ugia