如何设置请求标头从Android应用程序发送数据到我们的服务器

Jit*_*dar 8 java android json http-headers

如何设置http标头从我们的Android应用程序发送json对象 我们想用什么类型的标头从客户端发送数据到服务器.为什么我们使用标头,并在那个重要性.

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

                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

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

Mah*_*tha 15

你可以看看这个

你可以用它

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

post.addHeader( "X-Testing-Auth-Secret" , "kI7wGju76kjhJHGklk76" );
Run Code Online (Sandbox Code Playgroud)


Mah*_*tha 5

您可以使用

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();
    }
Run Code Online (Sandbox Code Playgroud)


Ser*_*m's 0

我在 .NET 中编写了一个 Web 服务,然后在我的 Android 应用程序中使用 ksoap2 将数据发送到我的服务器。

看看ksoap2-android:

http://simpligility.github.io/ksoap2-android/index.html

我建议添加这个库:

https://oss.sonatype.org/content/repositories/ksoap2-android-releases/com/google/code/ksoap2-android/ksoap2-android- assembly/3.0.0/ksoap2-android- assembly-3.0.0-带有依赖项的 jar.jar

它还带有所有依赖项。