警告/错误含义 - GbaRequest - GbaRequest:构造函数调用 222 userAgent Apache-HttpClient/UNAVAILABLE

2024-01-06

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

}


这本身并不是一个错误,Apache-HttpClient/不可用 (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 method

requestOrPost.setHeader("User-Agent", USER_AGENT);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

警告/错误含义 - GbaRequest - GbaRequest:构造函数调用 222 userAgent Apache-HttpClient/UNAVAILABLE 的相关文章

随机推荐