0

I have an activity which has 2 edittext controls and a button. User fills in the edittexts and taps on the button.

Here is the button onclick event:

public void submitHelpRequest(View v)
{
    final Button submitButton = (Button)findViewById(R.id.submithelprequest);
    submitButton.setEnabled(false);
    EditText titleElm;
    EditText messageElm;
    titleElm = (EditText)findViewById(R.id.helprequestitle);
    messageElm = (EditText)findViewById(R.id.helprequestmessage);
    String titleValue = titleElm.getText().toString();
    String messageValue = messageElm.getText().toString();
    new NWThread().execute("snip", titleValue, messageValue);
}

Here is my NWThread:

private class NWThread extends AsyncTask<String, Void, Void> {
    @Override
    protected Void doInBackground(String... values) {

        Post(values[0], values[1], values[2]);
        return null;
    }
}

Here is Post()

public void Post(String webAddress, final String title, final String message)
{
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(webAddress);

        String auth = new String(Base64.encode(( "snip" + ":" + "snip").getBytes(),Base64.URL_SAFE| Base64.NO_WRAP));
        httpPost.addHeader("Authorization", "Basic " + auth);

        String json = "";

        JSONObject object = new JSONObject();
        object.put("Title", title);
        object.put("Message", message);

        json = object.toString();

        StringEntity se = new StringEntity(json);

        httpPost.setEntity(se);
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/json");

        HttpResponse httpResponse = httpclient.execute(httpPost);

    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

I am getting some strange errors when i click on the button:

06-05 16:59:50.971 25135-2533 W/art: Failed to open zip archive '/system/framework/qcom.fmradio.jar': I/O Error
[ 06-05 16:59:50.971 25135:25332 W/] Unable to open '/system/framework/oem-services.jar': No such file or directory
06-05 16:59:50.971 25135-2533 W/art: Failed to open zip archive '/system/framework/oem-services.jar': I/O Error

Also in Android Studio it says that HttpClient and HttpPost are deprecated, could this be why? And if so which classes in the SDK should I be using?

Edit: Here is the whole activity

package ui;

import android.content.Context;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;

import java.net.HttpURLConnection;
import java.net.URL;

public class ScreenSendHelpRequestActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screen_send_help_request);
    }

    public void submitHelpRequest(View v)
    {
        final Button submitButton = (Button)findViewById(R.id.submithelprequest);
        submitButton.setEnabled(false);
        EditText titleElm;
        EditText messageElm;
        titleElm = (EditText)findViewById(R.id.helprequestitle);
        messageElm = (EditText)findViewById(R.id.helprequestmessage);
        String titleValue = titleElm.getText().toString();
        String messageValue = messageElm.getText().toString();

        new NWThread().execute("snip", titleValue, messageValue);
    }

    private class NWThread extends AsyncTask<String, Void, Void> {
        @Override
        protected Void doInBackground(String... urls) {

            Post(urls[0], urls[1], urls[2]);
            return null;
        }
    }

    public void Post(String webAddress, final String title, final String message)
    {
        try {
            URL url1 = new URL(webAddress);
            HttpURLConnection cnx = (HttpURLConnection)url1.openConnection();
            cnx.setRequestMethod("POST");
            cnx.setRequestProperty("Content-type", "application/json");
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(webAddress);

            String auth = new String(Base64.encode(( "snip" + ":" + "snip").getBytes(),Base64.URL_SAFE| Base64.NO_WRAP));
            httpPost.addHeader("Authorization", "Basic " + auth);

            String json = "";

            JSONObject object = new JSONObject();
            object.put("Title", title);
            object.put("Message", message);

            json = object.toString();

            StringEntity se = new StringEntity(json);

            httpPost.setEntity(se);
            httpPost.setHeader("Accept", "application/json");
            httpPost.setHeader("Content-type", "application/json");
            HttpResponse httpResponse = httpclient.execute(httpPost);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
andrewb
  • 2,995
  • 7
  • 54
  • 95

1 Answers1

0

Also in Android Studio it says that HttpClient and HttpPost are deprecated, could this be why? And if so which classes in the SDK should I be using?

You should use Volley Library to do API REST and HTTP call.

I'll write down an example from you using the code from one of my activity app. Please note that this example made a JSON request, is it possible also to do a StringRequest that returns a raw string (and also a JSONARRAYrequest).

    RequestQueue queue = Volley.newRequestQueue(this);
            String url = "https://www.google.it"; // your URI
            JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,url, null, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    //Handle positive response
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
//Handle negative response                   
} {
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    Map<String,String> headers = new HashMap<>();
//put values headers
                    headers.put("user",username.getText().toString());
                    return headers;
                }
            };
            queue.add(request);
Thecave3
  • 762
  • 12
  • 27
  • I originally use Volley but we want to use native objects instead of adding another library – andrewb Jun 06 '17 at 00:23
  • Native libraries as HttpClient are deprecated since there where a lot or issues. I think that volley (the official library now support​ed by Google ) and OkHttp (Java lib) are the best libraries to use to make http requests. – Thecave3 Jun 06 '17 at 00:27
  • Why do you think it's a big overhead? – Thecave3 Jun 06 '17 at 00:28
  • I don't but other developers on the project don't want it. – andrewb Jun 06 '17 at 00:35
  • Well, the native methods for all i know are deprecated. Use the library you prefer. [Here](https://stackoverflow.com/questions/16902716/comparison-of-android-networking-libraries-okhttp-retrofit-and-volley) is a guide to choose. And also if you want a concrete answer post the code of the Activity – Thecave3 Jun 06 '17 at 00:39