它是第二个 try/catch 块。Logcat 仍在记录 size<=0。我确实有来自服务器的响应。我不知道为什么会发生。谁能帮我?如果是这样,你能告诉我处理这个reposne的正确方法吗?
public class MainActivity extends Activity implements OnClickListener {
TextView tv;
EditText et;
String barcode;
String url = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView1);
et = (EditText) findViewById(R.id.editText1);
Button btn = (Button) findViewById(R.id.b1);
btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.b1:
// create URL------------------
barcode = et.getText().toString();
StringBuilder sb = new StringBuilder("http://cathedralbc.web44.net/firstPHPfile.php?item=");
sb.append(barcode);
url= sb.toString();
//-----------------------------
new DoInBackGround().execute(url);
break;
default:
break;
}
}
public class DoInBackGround extends AsyncTask<String, Void, String> {
String urlComplete;
BufferedReader br = null;
String result = "";
@Override
protected String doInBackground(String... params) {
this.urlComplete = params[0];
return getDataFrom();
}
final String getDataFrom(){
try {
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(urlComplete);
HttpResponse response = client.execute(httpPost);
HttpEntity entity = response.getEntity();
InputStream InEntity = entity.getContent();
Log.i("log_tag", "READING1 " + InEntity.toString());
try{
BufferedReader bufferInEtity = new BufferedReader(new InputStreamReader(InEntity,"iso-8859-1"),0);
StringBuilder sbResponse = new StringBuilder();
String line = null;
while ((line = bufferInEtity.readLine()) != null) {
sbResponse.append(line + "\n");
}
InEntity.close();
result = sbResponse.toString();
}catch(Exception e){
Log.e("log_tag", "READING " + e.getMessage());
}
} catch (Exception e) {
Log.e("log_tag", "HTTPCLIENT " + e.getMessage());
}
return result;
}
@Override
protected void onPostExecute(String result) {
try {
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length(); i++){
JSONObject jsonOBJ = jArray.getJSONObject(i);
tv.setText(jsonOBJ.getInt("name"));
}
} catch (JSONException e) {
Log.e("log_tag", "JSONArray " + e.getMessage());
}
}
}
}