我的代码有问题。当我单击按钮时,我收到消息 Invalid IP Address,但我真的不知道为什么。应用程序应该从 MySQL 数据库中填充微调器,但是当我按下按钮时,我会吐司“IP 无效”并且应用程序关闭。
public class IzboraGrada extends Activity {
Spinner spinner1, spinner2;
private Button button, izlaz;
static String str_grad,str_predmet,url;
InputStream is = null;
String result = null;
String line = null;
String[] grad, predmet;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_izbora_grada);
spinner1=(Spinner) findViewById(R.id.spinner1);
spinner2=(Spinner) findViewById(R.id.spinner2);
final List<String> list1=new ArrayList<String>();
final List<String> list2=new ArrayList<String>();
Button b=(Button) findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.1.2/test/spinner1.php");
HttpResponse response = httpclient.execute(httppost); //I think that problem is over here
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
{
JSONObject jsonObj=new JSONObject(result);
JSONArray JA=jsonObj.getJSONArray("lista");
JSONObject json= null;
grad = new String[JA.length()];
predmet = new String[JA.length()];
for(int i=0;i<JA.length();i++)
{
json=JA.getJSONObject(i);
grad[i] = json.getString("Grad");
predmet[i]=json.getString("Predmet");
}
Toast.makeText(getApplicationContext(), "sss",Toast.LENGTH_LONG).show();
for(int i=0;i<grad.length;i++)
{
list1.add(grad[i]);
list2.add(predmet[i]);
}
spinner_fn();
}
catch(Exception e)
{
Log.e("Fail 3", e.toString());
//login.this.finish();
}
}
});
}
private void spinner_fn() {
// TODO Auto-generated method stub
ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, grad);
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, predmet);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(dataAdapter2);
spinner1.setOnItemSelectedListener(new OnItemSelectedListener()
{
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() {
public void onItemSelected(AdapterView<?> arg0, View arg1,int position, long arg3) {
// TODO Auto-generated method stub
spinner1.setSelection(position);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
我的 PHP,当我在 localhost 中启动它时,返回:
{"lista":[{"Grad":"Beograd","Predmet":"matematika"},{"Grad":"Novi_Sad","Predmet":"matematika"},{"Grad":"Beograd","Predmet":"matematika"},{"Grad":"Novi_Sad","Predmet":"matematika"},{"Grad":"Beograd","Predmet":"informatika"},{"Grad":"Beograd","Predmet":"informatika"},{"Grad":"Beograd","Predmet":"engleski"},{"Grad":"Kragujevac","Predmet":"engleski"},{"Grad":"Kragujevac","Predmet":"matematika"},{"Grad":"Beograd","Predmet":"hemija"},{"Grad":"Kragujevac","Predmet":"fizika"},{"Grad":"Beograd","Predmet":"matematika"},{"Grad":"Beograd","Predmet":"engleski"},{"Grad":"Beograd","Predmet":"oet"},{"Grad":"Beograd","Predmet":"oet"},{"Grad":"Kragujevac","Predmet":"tehnicko_crtanje"},{"Grad":"Novi_Sad","Predmet":"matematika"},{"Grad":"Novi_Sad","Predmet":"informatika"},{"Grad":"Novi_Sad","Predmet":"matematika"},{"Grad":"Beograd","Predmet":"fizika"},{"Grad":"Kragujevac","Predmet":"francuski"}]}
并从 logcat 报告
07-29 21:05:35.832: D/dalvikvm(24075): GC_CONCURRENT freed 41K, 6% free 12530K/13319K, paused 14ms+2ms, total 43ms
07-29 21:05:35.832: D/dalvikvm(24075): WAIT_FOR_CONCURRENT_GC blocked 14ms
07-29 21:05:35.982: D/libEGL(24075): loaded /system/lib/egl/libEGL_mali.so
07-29 21:05:35.997: D/libEGL(24075): loaded /system/lib/egl/libGLESv1_CM_mali.so
07-29 21:05:36.002: D/libEGL(24075): loaded /system/lib/egl/libGLESv2_mali.so
07-29 21:05:36.017: D/(24075): Device driver API match
07-29 21:05:36.017: D/(24075): Device driver API version: 10
07-29 21:05:36.017: D/(24075): User space API version: 10
07-29 21:05:36.017: D/(24075): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Tue Oct 16 15:37:13 KST 2012
07-29 21:05:36.062: D/OpenGLRenderer(24075): Enabling debug mode 0
07-29 21:05:37.512: E/Fail 1(24075): android.os.NetworkOnMainThreadException
07-29 21:05:37.592: E/Fail 2(24075): java.lang.NullPointerException
07-29 21:05:37.592: E/Fail 3(24075): java.lang.NullPointerException