使用RESTful Web服务在TextView中显示数据

时间:2015-06-10 05:27:54

标签: android web-services rest

我正在尝试在RESTful Web服务中检索数据并在Textview中显示检索数据。我尝试这个代码,但当我运行它,它强制关闭或不幸的应用程序已停止。我的代码出了什么问题?

import android.os.AsyncTask;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

class Details extends ActionBarActivity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    protected void onStart() {
        super.onStart();
        new HttpRequestTask().execute();
    }
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_login, menu);
        return true;
    }
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_example) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
public void action(){
    new HttpRequestTask().execute();
}
public static class DriverDetails extends Fragment {
    View rootview;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootview = inflater.inflate(R.layout.activity_driver_details, container, false);
        return rootview;
    }
}
private class HttpRequestTask extends AsyncTask<Void, Void, DriverD> {
    protected DriverD doInBackground(Void... params) {
        try {
            final String url = "http://192.168.254.105:8080/taxisafe/platecheck/magcheck";
            RestTemplate restTemplate = new RestTemplate();
            restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
            DriverD driver = restTemplate.getForObject(url, DriverD.class);
            return driver;
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show();
        }

        return null;
    }
    protected void onPostExecute(DriverD driverD) {
        TextView greetingIdText = (TextView) findViewById(R.id.name);
        TextView greetingContentText = (TextView) findViewById(R.id.operator);
        greetingIdText.setText(driverD.getTaxi_name());
        greetingContentText.setText(driverD.getTaxi_plate_no());
    }

}
}

这是我的DriverD类

public class DriverD {
private String taxi_name;
private String taxi_plate_no;

public String getTaxi_name() {
    return this.taxi_name;
}
public String getTaxi_plate_no(){
    return this.taxi_plate_no;
}
}

0 个答案:

没有答案