I don’t understand how to transfer the received token to the HTTP request header to perform POST requests to api. It is necessary to transfer something like this:

Authorization: Token token
Where in the token you need to substitute the received token in the request header. But where to transfer the token in the code I do not understand ..

Below is the code to transfer the received token to.

public class ActivityTwo extends Activity { String b = "product1"; Context c; EditText Etext; EditText Erate; Button sendRevBtn; String textRev; String rateRev; String url = "http://smktesting.herokuapp.com/api/reviews/1"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.products_sign); TextView productName1 = (TextView) findViewById(R.id.productListName1); TextView productText = (TextView) findViewById(R.id.productListText1); ImageView image = (ImageView) findViewById(R.id.thumbnailImage1); Intent intent = getIntent(); Intent intent1 = getIntent(); String fName = intent.getStringExtra("fname"); String text1 = intent.getStringExtra("text"); final String token = intent1.getExtras().getString("token"); Toast.makeText(ActivityTwo.this, token, Toast.LENGTH_SHORT).show(); productName1.setText(fName); productText.setText(text1); if(getIntent().hasExtra("byteArray")) { Bitmap bitmam = BitmapFactory.decodeByteArray( getIntent().getByteArrayExtra("byteArray"), 0, getIntent().getByteArrayExtra("byteArray").length); image.setImageBitmap(bitmam); } //Лист для коментов ListView listV = (ListView) findViewById(R.id.comentsList); ComentsArrayAdapter adapter; Coments2ArrayAdapter adapter2; adapter = new ComentsArrayAdapter(this); adapter2 = new Coments2ArrayAdapter(this); if (productName1.getText().equals(b)) { listV.setAdapter(adapter); }else listV.setAdapter(adapter2); //тело Пост запроса коментариев // // c = this; Etext = (EditText) findViewById(R.id.input_text); Erate = (EditText) findViewById(R.id.input_rate); sendRevBtn = (Button) findViewById(R.id.send_rev); sendRevBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { textRev = Etext.getText() + ""; rateRev = Erate.getText() + ""; if ( textRev .length() == 0 || rateRev.length() == 0) { Toast.makeText(c, "Please fill in all fields", Toast.LENGTH_SHORT).show(); return; } if ( textRev.length() > 0 && rateRev.length() > 0) { //Do networking Networking n = new Networking(); n.execute(url, Networking.NETWORK_STATE_COMENTS); if ( token == null){ Toast.makeText(c, "Please login", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(c, "Review send", Toast.LENGTH_SHORT).show(); } } } }); } //AsyncTask good for long running tasks public class Networking extends AsyncTask { public static final int NETWORK_STATE_COMENTS = 1; @Override protected Object doInBackground(Object[] params) { getJson((String) params[0], (Integer) params[1]); return null; } } private void getJson(String url, int state) { //Do a HTTP POST, more secure than GET HttpClient httpClient = new DefaultHttpClient(); HttpPost request = new HttpPost(url); List<NameValuePair> postParameters = new ArrayList<NameValuePair>(); boolean valid = false; switch (state) { case Networking.NETWORK_STATE_COMENTS: //Building key value pairs to be accessed on web postParameters.add(new BasicNameValuePair("text", textRev)); postParameters.add(new BasicNameValuePair("rate", rateRev)); valid = true; break; default: Toast.makeText(c, "Unknown state", Toast.LENGTH_SHORT).show(); } if (valid == true) { //Reads everything that comes from server BufferedReader bufferedReader = null; StringBuffer stringBuffer = new StringBuffer(""); try { UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParameters); request.setEntity(entity); //Send off to server HttpResponse response = httpClient.execute(request); //Reads response and gets content bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); String line = ""; String LineSeparator = System.getProperty("line.separator"); //Read back server output while ((line = bufferedReader.readLine()) != null) { stringBuffer.append(line + LineSeparator); } bufferedReader.close(); } catch (Exception e) { e.printStackTrace(); } decodeResultIntoJson(stringBuffer.toString()); } else { } } private void decodeResultIntoJson(String response) { if (response.contains("error")) { try { JSONObject jo = new JSONObject(response); String error = jo.getString("error"); } catch (JSONException e) { e.printStackTrace(); } } try { JSONObject jo = new JSONObject(response); String success = jo.getString("success"); String message = jo.getString("message"); } catch (JSONException e) { e.printStackTrace(); } } } 

    1 answer 1

    In general, the header to the request is added as follows:

     HttpPost httpPost = new HttpPost(url); httpPost.addHeader("header-name" , "header-value"); 

    In your code, if I understood it correctly, you should use the following construction:

     HttpPost request = new HttpPost(url); if(token !== null){ request.addHeader("Authorization" , "Token " + token); }