Android アプリで次の /share リクエスト (POST) を行いたいと思います。 https://www.dropbox.com/developers/reference/api#shares
しかし、私は以前に http リクエストを行ったことがなく、方法がわかりません。
私のアプリは既に Dropbox で認証されています。
誰でもサンプルを提供できますか?
ps.私はhttpの理論を知っていますが、Javaでの実際の使用は知りません
Android アプリで次の /share リクエスト (POST) を行いたいと思います。 https://www.dropbox.com/developers/reference/api#shares
しかし、私は以前に http リクエストを行ったことがなく、方法がわかりません。
私のアプリは既に Dropbox で認証されています。
誰でもサンプルを提供できますか?
ps.私はhttpの理論を知っていますが、Javaでの実際の使用は知りません
私のアドバイスは、LoopJのようなライブラリを使用することです。「再試行のリクエスト」のように、自分で実装したくないものを処理します。このページにはすでに簡単な例があります。
ここで Android 開発者が推奨するように、HttpClient の代わりに HttpURLConnection を使用して ください。
サンプルは次のとおりです。
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();
Android では Http リクエストはこのように行われます. これは単なるサンプル コードです. 関連する多くのことを試してみてください.
役立つガイド: 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 サービスで 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();
}
};
}