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();
}
}
}