我在http://omnicoders.in/test/index.php有一个包含json数据的php文件。我试图解析文件并在textview中打印。但是当执行此文件时,不会打印任何内容。我想知道解析或index.php是否有问题,因为我是第一次使用它。
package com.example.connectingapp;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.TextView;
public class ConnectingMainActivity extends ActionBarActivity {
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_connecting_main);
tv = (TextView) findViewById(R.id.tv_intro);
String url = getString(R.string.url);
new Execution().execute(url);
}
class Execution extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(params[0]);
String overAll = "";
try {
HttpResponse response = httpClient.execute(httpPost);
BufferedReader reader = new BufferedReader(
new InputStreamReader(
response.getEntity().getContent(), "UTF-8"));
StringBuilder builder = new StringBuilder();
for (String line = null; (line = reader.readLine()) != null;) {
builder.append(line).append("\n");
}
JSONObject jsonObject = new JSONObject(builder.toString());
String name = jsonObject.getString("name");
String lastName = jsonObject.getString("lasr");
String mail = jsonObject.getString("mail");
overAll = name + " " + lastName + "\n" + mail;
} catch (Exception e) {
e.printStackTrace();
}
return overAll;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
tv.setText(result);
}
}
}
答案 0 :(得分:0)
此处在您的JSON数据中,name
实际上没有标记first
。更改您的代码以从JSONObject获取名称,它将起作用。
String name = jsonObject.getString("first"); // main change here
String lastName = jsonObject.getString("lasr");
String mail = jsonObject.getString("mail");
overAll = name + " " + lastName + "\n" + mail;
答案 1 :(得分:0)
使用此代码工作。如果ws没有输入数据则不要使用HttpPost
String url = "http://omnicoders.in/test/index.php";
new Execution().execute(url);
class Execution extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String overAll = "";
try {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
try {
response = httpclient.execute(new HttpGet(params[0]));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
JSONObject jsonObject = new JSONObject(out.toString());
String firstName = jsonObject.getString("first");
String lastName = jsonObject.getString("lasr");
String mail = jsonObject.getString("mail");
overAll = "Name: "+firstName + " " + lastName + "\nEmail:" + mail;
} else{
//Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (ClientProtocolException e) {
//TODO Handle problems..
} catch (IOException e) {
//TODO Handle problems..
}
return overAll;
} catch (Exception e) {
e.printStackTrace();
}
return overAll;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Log.e("Result:", result);
}
07-04 04:48:13.745:E /结果:(1404):姓名:vinit chouhan 07-04 04:48:13.745:E /结果:(1404):电子邮件:vinit.gbps@gmail.com