获得unsupportedEncodingException

时间:2014-01-21 08:24:26

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

我正在尝试使用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;


    }

}

Logcat: 01-21 14:01:40.637:I / System.out(1337):服务器响应 - 博客错误LoginSign-Up博客

未找到

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

Blog

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

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

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

请帮助解决此问题。从昨天开始尝试但没有得到正确的解决方法。

1 个答案:

答案 0 :(得分:0)

总是在POST检查状态代码后如下。

检查

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

我猜你得到500系列错误。