Опитвам се да използвам PHP уеб услуга с Android. Установяване на връзка със сървъра чрез HttpClient
. Когато изпълнявам кода, получавам HTML документ, вместо да получа JSON String, за да го анализирам допълнително. Не мога да разбера къде точно ми липсва. В 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): Server Response - Blog Errors LoginSign-Up Blog
Не е намерено
Error: The requested address '/respondents/Efair/registrations/add.php' was not found on this server. BlogЕто как извиквам горния метод.
registrationResponse = connector.postRegistrationData(userRegistrationBean);
Toast.makeText(getApplicationContext(), registrationResponse, Toast.LENGTH_SHORT).show();
userRegistrationBean
тук не е нищо друго освен контейнер за данни.
Моля, помогнете за разрешаването на този проблем. Опитвам се от вчера, но не намирам правилния начин да го реша.
unsupportedEncodingException
, но също така казвате, че получавате грешка, която не е намерена. Кое е? Ако не е намерено, тогава как, за бога, можем да помогнем да го поправим? - person Scary Wombat   schedule 22.01.2014