【发布时间】: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