发布请求JSON返回null

时间:2018-05-09 10:03:11

标签: android

朋友们,当我在邮递员中放入mu url并且我将3个参数放入体内时,我发现响应内容为代码,或者在我的varibale内容中,响应返回" 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:我打电话给我的班级,我看到了我在吐司留言中的可变性

在这里输入代码

 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();

        }
    });
  }
}

my result

0 个答案:

没有答案