【问题标题】:where can i learn to make http requests in android?我在哪里可以学习在 android 中发出 http 请求?
【发布时间】:2012-06-23 06:53:06
【问题描述】:

我希望我的 android 应用程序发出以下 /share 请求(POST)。 https://www.dropbox.com/developers/reference/api#shares

但我之前没有做过任何 http 请求,也不知道怎么做。

我的应用已通过 Dropbox 进行身份验证。

任何人都可以提供样品吗?

ps.我知道http的理论,但不知道它在java中的实际用途

【问题讨论】:

  • (这肯定是有据可查/教程的?)

标签: android http post http-post


【解决方案1】:

我的建议是使用 LoopJ 之类的库。它将处理您不想自己实现的事情,例如“请求重试”。它已经在此页面上提供了简单的示例。

http://loopj.com/android-async-http/

【讨论】:

    【解决方案2】:

    Http请求在Android中是这样完成的,它只是一个示例代码,你可以尝试很多相关的东西。

    实用指南:http://developer.android.com/reference/org/apache/http/client/HttpClient.html

            HttpClient httpclient = new DefaultHttpClient();   
            HttpPost httpPost = new HttpPost(YOUR_URL);
            HttpResponse response;
            try {
                response = httpclient.execute(httpPost); // the request executes
                Log.d("HTTP","Executed");
        String  responseBody = EntityUtils.toString(response.getEntity());
    
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            }
            catch(ConnectTimeoutException e){
            e.printStackTrace();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    

    希望对你有帮助

    【讨论】:

      【解决方案3】:

      您使用以下示例。

      此示例用于读取 http web 服务中的 json 字符串

      public class Httprequest_responseActivity extends Activity {
      ProgressDialog progressdialog;
      TextView txt;
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          txt=(TextView)findViewById(R.id.txt);
          httprequest("http://api.bilubi.com/BBService.svc/Compleateprogress");
      }
      String urlstr;
      public void httprequest(String url)
      {
          urlstr=url;
          progressdialog=ProgressDialog.show(Httprequest_responseActivity.this, "", "Loadding........",true);
          new Thread(new Runnable() {
      
              @Override
              public void run() {
                  BufferedReader in=null;
      
                  Message msg=Message.obtain();
                  msg.what=1;
                  try
                  {
                      HttpClient client=new DefaultHttpClient();
                      HttpGet request=new HttpGet();
                      request.setURI(new URI(urlstr));
                      HttpResponse response=client.execute(request);
                      in=new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                      StringBuffer sb=new StringBuffer("");
                      String line="";
                      while((line=in.readLine())!=null)
                          sb.append(line);
                      Bundle b=new Bundle();
                      b.putString("data", sb.toString());
                      msg.setData(b);
                      in.close();
      
                  }
                  catch(Exception e)
                  {
                      System.out.println("****************"+e.getMessage());
                      //txt.setText(""+e.getMessage());
                  }
      
                  handler.sendMessage(msg);
              }
          }).start(); 
      }
      
      Handler handler=new Handler()
      {
          public void handleMessage(Message msg)
          {
              super.handleMessage(msg);
              switch(msg.what)
              {
              case 1:
      
                  txt.setText(msg.getData().getString("data"));
                  break;
              }
              progressdialog.dismiss();
          }
      };
      

      }

      【讨论】:

        【解决方案4】:

        使用 HttpURLConnection 代替 HttpClient,这里是 android 开发者推荐的: http://android-developers.blogspot.in/2011/09/androids-http-clients.html

        示例如下:

        URL url = new URL("www.yandex.ru");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        InputStream in = new BufferedInputStream(connection.getInputStream());
        String response = new Scanner(in).useDelimiter("\\A").next();
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-09-06
          • 1970-01-01
          • 1970-01-01
          • 2015-07-07
          • 2022-08-17
          • 1970-01-01
          相关资源
          最近更新 更多