【问题标题】:Getting unsupportedEncodingException获取不支持的编码异常
【发布时间】:2014-01-22 13:34:49
【问题描述】:

我正在尝试使用 Android 使用 PHP Web 服务。通过HttpClient 建立与服务器的连接。当我执行代码时,我获取的是 HTML 文档,而不是获取 JSON 字符串来进一步解析它。无法弄清楚我到底缺少什么。 在 URL 中,当我删除 .php 扩展名时获取 null 并且添加 .php 扩展名时仅获取 HTML Documnet。代码如下,

public class ServerConnector {


    //HttpResponse response;
    public String postRegistrationData(UserRegistrationBean userRegistration){
        final String URL = "http://webserviceaddress.com/respondents/Efair/registrations/add.php";
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(URL);
        try{
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            Log.d("Name", userRegistration.getName());
            nameValuePairs.add(new BasicNameValuePair("name", userRegistration.getName()));
            Log.d("C_Name", userRegistration.getCompany_name());
            nameValuePairs.add(new BasicNameValuePair("company_name", userRegistration.getCompany_name()));
            Log.d("Email", userRegistration.getE_mail());
            nameValuePairs.add(new BasicNameValuePair("email", userRegistration.getE_mail()));
            Log.d("Product", userRegistration.getProd_of_interest());
            nameValuePairs.add(new BasicNameValuePair("product_of_interest", userRegistration.getProd_of_interest()));
            Log.d("Mobile ", userRegistration.getMobile_no());
            nameValuePairs.add(new BasicNameValuePair("mobile_no", userRegistration.getMobile_no()));
            nameValuePairs.add(new BasicNameValuePair("country", "India"));
            httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
            UrlEncodedFormEntity ent = new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8);
            httpPost.setEntity(ent);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            InputStream inputStream = httpResponse.getEntity().getContent();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            StringBuilder data = new StringBuilder();
            String bufferedStrChunk = null;
            while((bufferedStrChunk = bufferedReader.readLine()) != null){
                data.append(bufferedStrChunk);
            }
            System.out.println("Server Response - "+ data.toString());
            return data.toString();

        }catch(ClientProtocolException e){
            Log.d("RegistrationResponse", e.toString());
        }catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.d("RegistrationResponse", e.toString());
        }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.d("RegistrationResponse", e.toString());
        }
        return null;


    }

}

日志猫: 01-21 14:01:40.637: I/System.out(1337): 服务器响应 - 博客错误登录注册博客

未找到

错误: 在此服务器上未找到请求的地址 '/respondents/Efair/registrations/add.php'。博客

这就是我调用上述方法的方式。

registrationResponse = connector.postRegistrationData(userRegistrationBean);
Toast.makeText(getApplicationContext(), registrationResponse, Toast.LENGTH_SHORT).show();

userRegistrationBean 在这里只不过是一个数据容器。

请帮忙解决这个问题。从昨天开始尝试,但没有找到正确的方法来解决它。

【问题讨论】:

  • 请发布您的 logCat,以便我们查看引发此异常的位置
  • 如何在这里显示Logcat..它非常大
  • 只显示相关部分。将其保存到文件中,然后复制并粘贴。
  • 服务器响应是 HTML 文档,只要在 URL 中添加 .php 扩展名,否则什么也不返回。 Html Doc足够大,可以在这里发布..请帮助
  • 无法在此处粘贴...还有其他选项可以在此处发布 logcat ...?

标签: php android apache-httpclient-4.x


【解决方案1】:

始终在 POST 之后检查状态代码,如下所示。

检查

HttpResponse httpResp = client.execute(response);
int code = httpResp.getStatusLine().getStatusCode();

我猜你遇到了 500 系列错误。

【讨论】:

  • 不,我没有收到 500 系列错误,实际上我收到的是状态码 200。
  • 但是没有数据也没有错误?尝试使用wireshark进行监控。也许服务器没有向您发送任何响应?
  • 你能告诉我如何解决这个问题。现在我在 logcat 01-22 11:37:31.045 中收到此消息:D/SntpClient(59): request time failed: java.net.SocketException: Address family not supported by protocol.
  • 我解决了这个问题。这是服务器问题。服务器没有为我的查询发送响应。感谢您的帮助。
猜你喜欢
  • 2022-11-30
  • 1970-01-01
  • 1970-01-01
  • 2022-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多