Comment configurer l'en-tête http pour l'envoi d'objet Json à partir de nos applications Android Quel type d'en-tête nous voulons utiliser pour envoyer des données du côté client au serveur. un.
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
}
}
}
}
Je ne sais pas comment définir l'en-tête de requête pour l'envoi de données du côté client au serveur. Dans mon code, comment définir l'en-tête de requête
vous pouvez regarder dans ceci
tu peux utiliser ça
HttpPost post = new HttpPost( "http://wwww.testserver.com/userAddMoney" );
post.addHeader( "X-Testing-Auth-Secret" , "kI7wGju76kjhJHGklk76" );
vous pouvez utiliser
try {
HttpClient client = new DefaultHttpClient();
String getURL = "http://helloworld.com/getmethod.aspx?id=1&method=getData";
HttpGet httpGet = new HttpGet(getURL);
**httpGet .setHeader("Content-Type", "application/x-Zip");**
HttpResponse response = client.execute(httpGet);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
//parse response.
Log.e("Response",EntityUtils.toString(resEntity));
}
} catch (Exception e) {
e.printStackTrace();
}
essaye ça:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://floating-wildwood-1154.herokuapp.com/posts/create_a_post");
// Add Headers
httppost.addHeader("key1", "value1");
httppost.addHeader("key2", "value2");
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
}
J'ai codé un service Web dans .NET et ensuite utilisé ksoap2 dans mon application Android pour envoyer des données à mon serveur.
Regardez ksoap2-Android:
http://simpligility.github.io/ksoap2-Android/index.html
Je suggère d'ajouter cette bibliothèque:
cela porte aussi toutes les dépendances.