Android 通过侦听器将HTTP-JSON数据从asyncTask返回到Activity
我正在尝试将JSON数据从AsyncTask扩展类返回到活动。 我的活动代码如下:Android 通过侦听器将HTTP-JSON数据从asyncTask返回到Activity,android,json,Android,Json,我正在尝试将JSON数据从AsyncTask扩展类返回到活动。 我的活动代码如下: public class MainActivity extends Activity implements JSONListener{ private JSONObject jsonData = null; @Override public void JSONFeedBack(JSONObject jsonData) { // TOD
public class MainActivity extends Activity implements JSONListener{
private JSONObject jsonData = null;
@Override
public void JSONFeedBack(JSONObject jsonData) {
// TODO Auto-generated method stub
this.jsonData = jsonData;
Log.e("JSON check in JSONFeedBack(): ", jsonData.toString() );
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new JsonObj(this).execute("http://xml/url");
}
}
import org.json.JSONObject;
public interface JSONListener {
void JSONFeedBack(JSONObject jsonObj);
}
public class JsonObj extends AsyncTask<String, Void, JSONObject>{
MainActivity activity;
JSONListener jsonListener;
int tid;
String term;
public JsonObj(JSONListener jsonListener){
this.jsonListener = jsonListener;
}
@Override
protected JSONObject doInBackground(String... url) {
// TODO Auto-generated method stub
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost(url[0]);
JSONObject jsonObject = null;
// Depends on your web service
httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null){
sb.append(line + "\n");
}
result = sb.toString();
Log.e("JSON-Test [RESULT]: ", result);
jsonObject = new JSONObject(result);
} catch (Exception e) {
Log.e("JSON-Test [exception]: ", e.toString());
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
return jsonObject;
}
@Override
protected void onPostExecute(JSONObject result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
this.jsonListener.JSONFeedBack(result);
Log.e("OnPostExecute TEST: ", result.toString());
}
}
[
{
"data": {
"term": "All Nations Praise",
"tid": "10"
}
},
{
"data": {
"term": "Classified Advertisements",
"tid": "16"
}
},
{
"data": {
"term": "Kid's",
"tid": "11"
}
},
{
"data": {
"term": "KT Creative",
"tid": "9"
}
},
{
"data": {
"term": "KT Diary",
"tid": "8"
}
},
{
"data": {
"term": "Live at the Coronet",
"tid": "14"
}
},
{
"data": {
"term": "Situations Vacant",
"tid": "15"
}
},
{
"data": {
"term": "X:Change",
"tid": "13"
}
},
{
"data": {
"term": "Youth",
"tid": "12"
}
}
]
String line = null;
while ((line = reader.readLine()) != null){
sb.append(line); // remove + "\n" here
}
我的JSONListener如下所示:
public class MainActivity extends Activity implements JSONListener{
private JSONObject jsonData = null;
@Override
public void JSONFeedBack(JSONObject jsonData) {
// TODO Auto-generated method stub
this.jsonData = jsonData;
Log.e("JSON check in JSONFeedBack(): ", jsonData.toString() );
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new JsonObj(this).execute("http://xml/url");
}
}
import org.json.JSONObject;
public interface JSONListener {
void JSONFeedBack(JSONObject jsonObj);
}
public class JsonObj extends AsyncTask<String, Void, JSONObject>{
MainActivity activity;
JSONListener jsonListener;
int tid;
String term;
public JsonObj(JSONListener jsonListener){
this.jsonListener = jsonListener;
}
@Override
protected JSONObject doInBackground(String... url) {
// TODO Auto-generated method stub
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost(url[0]);
JSONObject jsonObject = null;
// Depends on your web service
httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null){
sb.append(line + "\n");
}
result = sb.toString();
Log.e("JSON-Test [RESULT]: ", result);
jsonObject = new JSONObject(result);
} catch (Exception e) {
Log.e("JSON-Test [exception]: ", e.toString());
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
return jsonObject;
}
@Override
protected void onPostExecute(JSONObject result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
this.jsonListener.JSONFeedBack(result);
Log.e("OnPostExecute TEST: ", result.toString());
}
}
[
{
"data": {
"term": "All Nations Praise",
"tid": "10"
}
},
{
"data": {
"term": "Classified Advertisements",
"tid": "16"
}
},
{
"data": {
"term": "Kid's",
"tid": "11"
}
},
{
"data": {
"term": "KT Creative",
"tid": "9"
}
},
{
"data": {
"term": "KT Diary",
"tid": "8"
}
},
{
"data": {
"term": "Live at the Coronet",
"tid": "14"
}
},
{
"data": {
"term": "Situations Vacant",
"tid": "15"
}
},
{
"data": {
"term": "X:Change",
"tid": "13"
}
},
{
"data": {
"term": "Youth",
"tid": "12"
}
}
]
String line = null;
while ((line = reader.readLine()) != null){
sb.append(line); // remove + "\n" here
}
我的asyncTask类如下所示:
public class MainActivity extends Activity implements JSONListener{
private JSONObject jsonData = null;
@Override
public void JSONFeedBack(JSONObject jsonData) {
// TODO Auto-generated method stub
this.jsonData = jsonData;
Log.e("JSON check in JSONFeedBack(): ", jsonData.toString() );
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new JsonObj(this).execute("http://xml/url");
}
}
import org.json.JSONObject;
public interface JSONListener {
void JSONFeedBack(JSONObject jsonObj);
}
public class JsonObj extends AsyncTask<String, Void, JSONObject>{
MainActivity activity;
JSONListener jsonListener;
int tid;
String term;
public JsonObj(JSONListener jsonListener){
this.jsonListener = jsonListener;
}
@Override
protected JSONObject doInBackground(String... url) {
// TODO Auto-generated method stub
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost(url[0]);
JSONObject jsonObject = null;
// Depends on your web service
httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null){
sb.append(line + "\n");
}
result = sb.toString();
Log.e("JSON-Test [RESULT]: ", result);
jsonObject = new JSONObject(result);
} catch (Exception e) {
Log.e("JSON-Test [exception]: ", e.toString());
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
return jsonObject;
}
@Override
protected void onPostExecute(JSONObject result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
this.jsonListener.JSONFeedBack(result);
Log.e("OnPostExecute TEST: ", result.toString());
}
}
[
{
"data": {
"term": "All Nations Praise",
"tid": "10"
}
},
{
"data": {
"term": "Classified Advertisements",
"tid": "16"
}
},
{
"data": {
"term": "Kid's",
"tid": "11"
}
},
{
"data": {
"term": "KT Creative",
"tid": "9"
}
},
{
"data": {
"term": "KT Diary",
"tid": "8"
}
},
{
"data": {
"term": "Live at the Coronet",
"tid": "14"
}
},
{
"data": {
"term": "Situations Vacant",
"tid": "15"
}
},
{
"data": {
"term": "X:Change",
"tid": "13"
}
},
{
"data": {
"term": "Youth",
"tid": "12"
}
}
]
String line = null;
while ((line = reader.readLine()) != null){
sb.append(line); // remove + "\n" here
}
因此,在调用Log.e(“JSON检入JSONFeedBack():”,jsonData.toString())时,我得到一个空指针异常代码>在我的JSONFeedBack
在我的活动类中
String line = null;
while ((line = reader.readLine()) != null){
sb.append(line); // remove + "\n" here
}
我已确认JSON数据是在AsynchClass中获取的,但我尝试将其发送到activity时,却一直导致空指针异常。我刚刚运行了您的代码,使用我的一项服务更改url,效果很好。。。我认为在创建JSONObject
jsonObject = new JSONObject(result);
String line = null;
while ((line = reader.readLine()) != null){
sb.append(line); // remove + "\n" here
}
是否确实不记录此行:
Log.e("JSON-Test [exception]: ", e.toString());
String line = null;
while ((line = reader.readLine()) != null){
sb.append(line); // remove + "\n" here
}
i、 e.如果我使用这个url,我会得到一个异常,创建JSONObject
,然后创建您得到的NPE
String line = null;
while ((line = reader.readLine()) != null){
sb.append(line); // remove + "\n" here
}
我很确定AsynchTask和Activity之间的通信是有效的,问题在于您的响应或如何解析它。不要在Json字符串中添加新行字符
String line = null;
while ((line = reader.readLine()) != null){
sb.append(line); // remove + "\n" here
}
编辑:您应该在Log.e(“JSON测试[exception]:”,e.toString())中看到一个异常,因为这一点代码>您是否尝试将Log.e(“OnPostExecute测试:,result.toString())
beforethis.jsonListener.JSONFeedBack(结果)
确保结果
不为空?谢谢您的回答。我对你的代码感到非常困惑,这可能证实了我的json数据有问题的怀疑。让我知道你的想法。我粘贴了有问题的JSON输出
String line = null;
while ((line = reader.readLine()) != null){
sb.append(line); // remove + "\n" here
}