Пожалуйста, следуйте приведенному ниже коду.Здесь я отправляю запрос на сервер и получаю этот ответ и анализирую этот ответ в формате json.
public void Login(final String UserName,final String Password)
{
try
{
new Thread()
{
public void run()
{
try
{
HttpPost postMethod = new HttpPost("Your Url");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("UserName","Your UserName");
nameValuePairs.add(new BasicNameValuePair("Password", "Your Password"));
postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
DefaultHttpClient hc = new DefaultHttpClient();
HttpResponse response = hc.execute(postMethod);
HttpEntity entity = response.getEntity();
// If the response does not enclose an entity, there is no need
// to worry about connection release
if (entity != null)
{
InputStream inStream = entity.getContent();
result= convertStreamToString(inStream);
jsonObject = new JSONObject(result);
responseHandler.sendEmptyMessage(0);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}.start();
responseHandler = new Handler()
{
public void handleMessage(Message msg)
{
super.handleMessage(msg);
try
{
Log.i("-------------------- Response",result);
if(jsonObject.getJSONObject("response").getString("msg").equalsIgnoreCase("Success"))
{
Log.i("Login","--- Login Success");
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
};
}
catch(Exception e)
{
}
}
public static String convertStreamToString(InputStream is)
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try
{
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
is.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
return sb.toString();
}