我想我的Android應用程序進行以下/共享請求(POST)。 https://www.dropbox.com/developers/reference/api#shares我可以在哪裏學習在android中創建http請求?
但我以前沒有做過任何HTTP請求,不知道如何。
我的應用已經與Dropbox的認證。
有人可以提供樣品嗎?
ps.i知道在java中http.But不是它的實際用途
我想我的Android應用程序進行以下/共享請求(POST)。 https://www.dropbox.com/developers/reference/api#shares我可以在哪裏學習在android中創建http請求?
但我以前沒有做過任何HTTP請求,不知道如何。
我的應用已經與Dropbox的認證。
有人可以提供樣品嗎?
ps.i知道在java中http.But不是它的實際用途
我的建議是使用諸如循環J庫的理論。它將處理你不想實施的事情,如「請求重試」。它帶有簡單的例子,已經在這個頁面上。
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;
}
希望這有助於
您使用下面的示例。
這個例子中HTTP Web服務
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();
}
};
}
使用HttpURLConnection類用於讀取JSON字符串,而不是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();
(當然噸他有很好的文檔記錄/教程?) – 2012-06-23 06:58:23