1

任何人都可以帮我解决这个问题。我正在尝试通过 Web API 在服务器中获取信息,我相信,在下面的代码中,我已经可以连接到服务器(因为没有出现错误)。但是当我试图显示我得到的信息时,它会显示空值。我不确定在哪里忘记了某些东西,或者我的解析方式是否正确。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    buttonGetData = (Button) findViewById(R.id.buttonGetData);
    editTextSearchString = (EditText) findViewById(R.id.editTextSearchString);
    textViewFirstName = (TextView) findViewById(R.id.textViewFirstName);
    textViewLastName = (TextView) findViewById(R.id.textViewLastName);
    display = (TextView) findViewById(R.id.display);
    spn_Display = (Spinner)findViewById(R.id.spn_Display);
    //Setup the Button's OnClickListener
    buttonGetData.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            //Get the data
            in = null;
            DoPOST mDoPOST = new DoPOST(MainActivity.this, editTextSearchString.getText().toString());
            Toast.makeText(getApplicationContext(), editTextSearchString.getText().toString(), 6).show();
            mDoPOST.execute("");
            buttonGetData.setEnabled(false);

        }
    });

}

public class DoPOST extends AsyncTask<String, Void, Boolean>
{

    Context mContext = null;
    String strNameToSearch = "";

    //Result data
    String strFirstName;
    String strLastName;
    int intAge;
    int intPoints;

    Exception exception = null;

    DoPOST(Context context, String nameToSearch){
        mContext = context;
        strNameToSearch = nameToSearch;
    }

    @Override
    protected Boolean doInBackground(String... arg0) {
        try{
            //Setup the parameters
            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("Username", "admin"));    
            nameValuePairs.add(new BasicNameValuePair("Password", "admin123")); 

            //Create the HTTP request
            HttpParams httpParameters = new BasicHttpParams();

            //Setup timeouts
            HttpConnectionParams.setConnectionTimeout(httpParameters, 15000);
            HttpConnectionParams.setSoTimeout(httpParameters, 15000);           

            HttpClient httpclient = new DefaultHttpClient(httpParameters);
            HttpPost httppost = new HttpPost("http://examplesvr4.sample.com:1217/api/subbrands");   
            HttpGet httpget = new HttpGet("http://examplesvr4.sample.com:1217/api/subbrands");

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));        
            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();

            result = EntityUtils.toString(entity);

            // Create a JSON object from the request response
            JSONObject jsonObject = new JSONObject(result);

            //Retrieve the data from the JSON object
            strFirstName = jsonObject.getString("SubBrandId");
            strLastName = jsonObject.getString("SubBrandName");

        }catch (Exception e){
            Log.e("ClientServerDemo", "Error:", e);
            exception = e;
        }

        return true;
    }

    @Override
    protected void onPostExecute(Boolean valid){
        //Update the UI
        textViewFirstName.setText("First Name: " + strFirstName);
        textViewLastName.setText("Last Name: " + strLastName);
        buttonGetData.setEnabled(true);

        if(exception != null){
            Toast.makeText(mContext, exception.getMessage(), Toast.LENGTH_LONG).show();
        }
    }

}
4

0 回答 0