【问题标题】:How send http request in android app to access REST API如何在 Android 应用程序中发送 http 请求以访问 REST API
【发布时间】:2014-01-27 06:39:57
【问题描述】:

谁能解决我的问题。我想在android中发送一个http请求来访问 REST API(PHP)..

谢谢

【问题讨论】:

标签: android


【解决方案1】:

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()  
} 

【讨论】:

  • Log.i(tag, result);这行是什么意思?
  • Log.i 正在向您的计算机发送“信息消息”,因此您可以轻松查看结果。
  • 如果我必须使用 HTTPS,我必须在上面进行哪些更改?或者它会正常工作???
  • 嘿,我不明白这一行..new HttpGet(URL + q)..我们实例化了 HttpGET 的新对象..但是什么是“q”?
【解决方案2】:

这基本上取决于您需要什么,但假设一个简单的带有 JSON 正文的 POST 请求,它看起来像这样(我建议使用 Apache HTTP 库)。

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();
}

最后,我鼓励您在任何类型的后台线程(执行程序、线程、异步任务等)中运行此代码

【讨论】:

    猜你喜欢
    • 2020-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-26
    • 2022-10-23
    • 2014-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多