我正在尝试从JSON响应中填充微调器。在两个微调器之后,我正在尝试读取JSON并填充它。但它没有发生。我的代码在
之下public class MainActivity extends Activity {
InputStream is=null;
String result=null;
String line=null;
String[] roll_no,name;
Spinner spinner1,spinner2;
final List<String> list1 = new ArrayList<String>();
final List<String> list2 = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
Button b=(Button) findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
new insertTask().execute("");
}
});
}
private void spinner_fn() {
// TODO Auto-generated method stub
ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_spinner_item, roll_no);
dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(dataAdapter1);
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_spinner_item, name);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(dataAdapter2);
spinner1.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int position, long id)
{
// TODO Auto-generated method stub
spinner2.setSelection(position);
}
@Override
public void onNothingSelected(AdapterView<?> arg0)
{
// TODO Auto-generated method stub
}
});
spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int position, long arg3) {
// TODO Auto-generated method stub
spinner1.setSelection(position);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
class insertTask extends AsyncTask<String, String, String> {
protected String doInBackground(String... params) {
String result = insert();
return result ;
}
protected void onPostExecute(String result) {
if(result.equals("OK")){
Toast.makeText(getBaseContext(), "Value Added",
Toast.LENGTH_LONG).show();
}
//after background is done, use this to show or hide dialogs
}
}
public String insert()
{
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://122.181.3.82:4007/CatchInfo/values");
HttpResponse response = httpclient.execute(httppost);
Log.e("Fail 1", "3");
HttpEntity entity = response.getEntity();
Log.e("Fail 1", "4");
is = entity.getContent();
Log.e("Pass 1", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 1", e.toString());
Toast.makeText(getApplicationContext(), "Invalid IP Address",Toast.LENGTH_LONG).show();
finish();
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result = sb.toString();
}
catch(Exception e)
{
Log.e("Fail 2", e.toString());
}
try
{
JSONArray JA=new JSONArray(result);
JSONObject json= null;
roll_no = new String[JA.length()];
name = new String[JA.length()];
for(int i=0;i<JA.length();i++)
{
json=JA.getJSONObject(i);
roll_no[i] = json.getString("roll_no");
name[i]=json.getString("name");
}
Toast.makeText(getApplicationContext(), "sss",Toast.LENGTH_LONG).show();
for(int i=0;i<roll_no.length;i++)
{
list1.add(roll_no[i]);
list2.add(name[i]);
}
spinner_fn();
}
catch(Exception e)
{
Log.e("Fail 3", e.toString());
//login.this.finish();
}
return result;
}
}
但是我收到这样的错误:
05-22 19:10:29.381: E/Fail 1(2136): 3
05-22 19:10:29.381: E/Fail 1(2136): 4
05-22 19:10:29.381: E/Pass 1(2136): connection success
05-22 19:10:29.451: E/Fail 3(2136): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
请帮帮我。