gpt4 book ai didi

java - 如何设置请求 header 以将数据从 Android 应用程序发送到我们的服务器

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:50:52 25 4
gpt4 key购买 nike

如何设置 http header 以从我们的 android 应用程序发送 json 对象我们希望使用哪种类型的 header 将数据从客户端发送到服务器。为什么我们要使用 header 以及该 header 的重要性。

public class HomeLayoutActivity extends Activity implements OnClickListener{

private EditText value;
private Button btn;
private ProgressBar pb;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_layout);
value=(EditText)findViewById(R.id.editText1);
btn=(Button)findViewById(R.id.button1);
pb=(ProgressBar)findViewById(R.id.progressBar1);
pb.setVisibility(View.GONE);
btn.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.home_layout, menu);
return true;
}

public void onClick(View v) {
// TODO Auto-generated method stub
if(value.getText().toString().length()<1){

// out of range
Toast.makeText(this, "please enter something", Toast.LENGTH_LONG).show();
}else{
pb.setVisibility(View.VISIBLE);
new MyAsyncTask().execute(value.getText().toString());
}


}

private class MyAsyncTask extends AsyncTask<String, Integer, Double>{

@Override
protected Double doInBackground(String... params) {
// TODO Auto-generated method stub
postData(params[0]);
return null;
}

protected void onPostExecute(Double result){
pb.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), "command sent", Toast.LENGTH_LONG).show();
}
protected void onProgressUpdate(Integer... progress){
pb.setProgress(progress[0]);
}

public void postData(String valueIWantToSend) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://floating-wildwood-1154.herokuapp.com/posts/create_a_post");

try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("content", valueIWantToSend));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);

} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block

}
}
}
}

我不知道如何设置从客户端向服务器端发送数据的请求 header 。在我的代码中如何设置请求 header

最佳答案

你可以查看this

你可以用这个

HttpPost post = new HttpPost( "http://wwww.testserver.com/userAddMoney" );

post.addHeader( "X-Testing-Auth-Secret" , "kI7wGju76kjhJHGklk76" );

关于java - 如何设置请求 header 以将数据从 Android 应用程序发送到我们的服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16793647/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com