我有一个活动,它有2个edittext控件和一个按钮。用户填写edittexts并点击按钮。
这是按钮onclick事件:
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);
}
这是我的NWThread:
private class NWThread extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... values) {
Post(values[0], values[1], values[2]);
return null;
}
}
这是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();
}
}
点击按钮后,我收到一些奇怪的错误:
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
同样在Android Studio中,它说HttpClient和HttpPost已被弃用,这可能是为什么?如果是这样,我应该使用SDK中的哪些类?
编辑:这是整个活动
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();
}
}
}
答案 0 :(得分:0)
同样在Android Studio中,它说HttpClient和HttpPost已被弃用,这可能是为什么?如果是这样,我应该使用SDK中的哪些类?
您应该使用Volley Library进行API REST和HTTP调用。
我将使用我的一个活动app中的代码向您写下一个示例。 请注意,此示例发出了JSON请求,是否也可以执行返回原始字符串的StringRequest(以及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);