【问题标题】:Android http post results in strange I/O errorAndroid http post 导致奇怪的 I/O 错误
【发布时间】:2017-06-05 23:10:23
【问题描述】:

我有一个包含 2 个编辑文本控件和一个按钮的活动。用户填写编辑文本并点击按钮。

这里是按钮的点击事件:

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

【问题讨论】:

  • 你应该使用Volley
  • 这个问题似乎也与您发布的代码无关,能否请您发布整个堆栈跟踪?
  • 贴上你的Activity代码就好了,至少你应该把你的onCreate方法的代码贴在Activity里,因为它和你的view有关。
  • Volley 不是一个选项,因为我们不希望另一个库的开销。我最初使用的是 Volley

标签: android


【解决方案1】:

同样在 Android Studio 中,它说 HttpClient 和 HttpPost 已被弃用,这可能是为什么?如果是这样,我应该使用 SDK 中的哪些类?

您应该使用Volley Library 进行 API REST 和 HTTP 调用。

我将使用我的活动app 中的代码写下您的示例。 请注意,此示例发出了 JSON 请求,是否也可以执行返回原始字符串(以及 JSONARRAY 请求)的 StringRequest。

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

【讨论】:

  • 我最初使用 Volley,但我们想使用原生对象而不是添加另一个库
  • 原生库作为 HttpClient 已被弃用,因为那里有很多或问题。我认为 volley(Google 现在支持的官方库)和 OkHttp(Java 库)是用于发出 http 请求的最佳库。
  • 为什么你认为这是一个很大的开销?
  • 我不想要,但该项目的其他开发人员不想要它。
  • 好吧,我所知道的本机方法已被弃用。使用您喜欢的库。 Here 是一个选择指南。如果您想要一个具体的答案,请发布活动的代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-03
  • 1970-01-01
  • 1970-01-01
  • 2012-02-28
  • 2021-04-10
相关资源
最近更新 更多