我在服务器上发布值并获得响应。但问题是它显示我为null作为响应。我正在做的错误。我尝试了很多方法但仍然没有找到任何解决方案。任何人都可以帮助我。 以下是我的代码 -
class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
String OTPTxt = otp.getText().toString();
int CustID = 2;
@Override
protected String doInBackground(String... params) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("mylink?CustID=" + CustID + "&OTP=" + OTPTxt);
List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
try {
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(nameValuePairList);
httpPost.setEntity(urlEncodedFormEntity);
HttpResponse httpResponse = httpClient.execute(httpPost);
InputStream inputStream = httpResponse.getEntity().getContent();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder stringBuilder = new StringBuilder();
String bufferedStrChunk = null;
while ((bufferedStrChunk = bufferedReader.readLine()) != null) {
stringBuilder.append(bufferedStrChunk);
}
return stringBuilder.toString();
} catch (ClientProtocolException cpe) {
System.out.println("Firstption caz of HttpResponese :" + cpe);
cpe.printStackTrace();
} catch (IOException ioe) {
System.out.println("Secondption caz of HttpResponse :" + ioe);
ioe.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(OTPScreen1.this, "Posting Data to server", Toast.LENGTH_SHORT).show();
Toast.makeText(OTPScreen1.this, "posting data " + responseServer, Toast.LENGTH_SHORT).show();
}
});
}
}
在buttonclick上调用了这个方法
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new SendPostReqAsyncTask().execute();
}
});