5
W/System.err(27207): [DEBUG] GbaRequest - GbaRequest: Constructor Called 222 userAgent Apache-HttpClient/UNAVAILABLE (java 1.4)

预先感谢您的帮助。我无法找到有关我在项目中收到的此错误的帖子。

我只是有时会收到此错误,但我不确定它为什么会出现,因为它在发生时似乎是随机的。我没有注意到我的数据输入中有任何异常。

我的 android 应用程序正在尝试连接到远程服务器并将数据推送到 PostgreSQL 表中。任何人都可以向我推荐此错误的正确文档或解释其含义。我感谢您的帮助。

这是我的代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JSONParser 
{
    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    public JSONParser() 
    {
        // Empty Constructor
    }

    public JSONObject getJSONFromUrl(String url) 
    {
        try 
        {
            DefaultHttpClient httpClient = new DefaultHttpClient();
                httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, System.getProperty("http.agent"));
            HttpPost httpPost = new HttpPost(url);
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
        } 
        catch(UnsupportedEncodingException e) 
        {
            e.printStackTrace();
        } 
        catch(ClientProtocolException e) 
        {
            e.printStackTrace();
        } 
        catch(IOException e) 
        {
            e.printStackTrace();
        }

        try 
        {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);

            StringBuilder sb = new StringBuilder();

            String line = null;

            while ((line = reader.readLine()) != null) 
            {
                sb.append(line + "\n");
            }

            is.close();

            json = sb.toString();
        } 
        catch (Exception e) 
        {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        try 
        {
            jObj = new JSONObject(json);
        } 
        catch (JSONException e) 
        {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        return jObj;
    }

    public JSONObject makeHttpRequest(String url, String method, List<NameValuePair> params) 
    {
        try 
        {
            if (method == "POST") 
            {
                DefaultHttpClient httpClient = new DefaultHttpClient();
                    httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, System.getProperty("http.agent"));
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));
                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
            } 
            else if (method == "GET") 
            {
                DefaultHttpClient httpClient = new DefaultHttpClient();
                    httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, System.getProperty("http.agent"));
                String paramString = URLEncodedUtils.format(params, "utf-8");
                url += "?" + paramString;
                HttpGet httpGet = new HttpGet(url);
                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
            }
        }
        catch (UnsupportedEncodingException e) 
        {
            e.printStackTrace();
        } 
        catch (ClientProtocolException e) 
        {
            e.printStackTrace();
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }

        try 
        {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
            is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;

            while ((line = reader.readLine()) != null) 
            {
                sb.append(line + "\n");
            }

            is.close();
            json = sb.toString();
        } 


catch (Exception e) 
    {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try 
    {
        jObj = new JSONObject(json);
    } 
    catch (JSONException e) 
    {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    return jObj;
}

}

4

1 回答 1

3

这本身不是错误,Apache-HttpClient/UNAVAILABLE (java 1.4)是 Apache HttpClient 的默认用户代理字符串。

当您不通过 HTTP 标头发送用户代理(“User-Agent:”)时会发生这种情况,然后电话 GBA 服务会警告您。

如果你想发送默认的系统用户代理,你可以这样做

httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, System.getProperty("http.agent"));

这应该像

 Dalvik/1.6.0 (Linux; U; Android 4.2.2; Galaxy Nexus Build/JDQ39)

或者,如果您想发送自定义用户代理,您可以这样做

httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "My user agent");

setHeader或者您可以通过方法设置标题

requestOrPost.setHeader("User-Agent", USER_AGENT);
于 2015-03-19T11:25:07.900 回答