android - 发布请求 JSON 返回 null
问题描述
当我将 mu url 放入邮递员并将我的 3 个参数放入正文时,我发现响应内容是代码或在 android 中我的变量内容响应返回“null”
我不明白为什么。
我的课:
public class task extends AsyncTask<URL, Void, Void> {
private Context applicationContext;
public HttpEntity responseBody;
public static String respons;
MainActivity activity;
public task(MainActivity activity){
this.activity=activity;
}
@Override
public Void doInBackground(URL... voids) {
respons=new String("");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://recrutement.stb.com.tn:1010/PlateformeApi_Externe/api/");
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList< NameValuePair >(5);
nameValuePairs.add(new BasicNameValuePair("username", "sami"));
nameValuePairs.add(new BasicNameValuePair("password", "stb"));
nameValuePairs.add(new BasicNameValuePair("AuthMethod",
"FormsAuthentication"));
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
Log.d("myapp", "works till here. 2");
try {
HttpResponse response = httpclient.execute(httppost);
Log.d("myapp", "response " + EntityUtils.toString(response.getEntity()));
respons = String.valueOf((response.getEntity()));
// respons=response.
// responseBody=(response.getEntity());
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
String s=respons;
}
@Override
protected void onPreExecute() {
//Show something on ui like Progress Dialog etc here
super.onPreExecute();
}
public Context getApplicationContext() {
return applicationContext;
}
}
MainActivity :我打电话给我的班级,我在 toast 消息中看到了我的变量
public class MainActivity extends AppCompatActivity {
Button bo;
URL url;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bo=findViewById(R.id.button);
bo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
task logIn = new task(MainActivity.this);
logIn.execute();
Toast.makeText(MainActivity.this,"code="+logIn.respons,Toast.LENGTH_LONG).show();
}
});
}
}
解决方案
推荐阅读
- mysql - 错误 1005 (HY000) mySql,在小型数据库上
- python-3.x - Python 3 - 使用 concurrent.futures 并行调用 .exe 的多个实例
- javascript - 样式滚动条仅在 Windows 上,不在 Mac 上
- c++11 - 使用 Wasm-ld 链接库时出错:std 函数上的符号类型 mimsatch
- google-admin-sdk - 目录 API 缺少用户的“网站 []”对象
- sql - 试图在 Laravel 中发布一个对象数组。ErrorException:从文件中的空值创建默认对象
- javascript - 为什么 useEffect 呈现意外的值?
- php - 将单选按钮添加到标准 Wordpress 注册表单
- laravel - 无法解码 json laravel
- android - 关闭 android 应用程序时忽略 Form.destroy