我希望我的Android应用程序发出以下/共享请求(POST)。 https://www.dropbox.com/developers/reference/api#shares
但我以前没有做任何http请求,也不知道如何。
我的应用已经使用Dropbox进行了身份验证。
有人可以提供样品吗?
ps.i知道http的理论但不是它在java中的实际用途
答案 0 :(得分:2)
我的建议是使用像LoopJ这样的库。它将处理你不想自己实现的事情,如“请求重试”。它附带了本页面上的简单示例。
答案 1 :(得分:1)
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;
}
希望这有助于
答案 2 :(得分:1)
您使用以下示例。
此示例用于读取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();
}
};
}
答案 3 :(得分:1)
使用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();