在做一个登录页面并且我将一些参数传递给JSONParser但它似乎没有添加params为什么?
这是代码: 此方法设置参数
public JSONObject loginUser(String login, String password){
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("secret", "7658474y33"));
params.add(new BasicNameValuePair("login", login));
params.add(new BasicNameValuePair("password", password));
params.add(new BasicNameValuePair("ping", "1"));
params.add(new BasicNameValuePair("retina", "1"));
params.add(new BasicNameValuePair("device_token", ""));
params.add(new BasicNameValuePair("device_name", ""));
params.add(new BasicNameValuePair("device_model", ""));
params.add(new BasicNameValuePair("device_sys", ""));
JSONObject json = jsonParser.getJSONFromUrl(loginURL, params);
这是JSONParser类
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import com.loopj.android.http.JsonHttpResponseHandler;
import android.util.Log;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
Log.i("JSONParser", httpPost.getURI().toString());
Log.i("param.size", Integer.toString(params.size()));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
Log.i("JSONParser", "is ok");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
Log.i("JSONParserException", "1");
} catch (ClientProtocolException e) {
e.printStackTrace();
Log.i("JSONParserException", "2");
} catch (IOException e) {
e.printStackTrace();
Log.i("JSONParserException", "3");
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "n");
}
is.close();
json = sb.toString();
Log.e("JSON", json);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
我收到此错误
android.os.NetworkOnMainThreadException
答案 0 :(得分:2)
如果您收到 android.os.NetworkOnMainThreadException 错误
这意味着您在主UI线程中执行您的功能。但你不能在UI`线程中使用网络...
为此,您必须使用 AsyncTask 或使用使用处理程序的线程或 runOnUiThread()来更新您的UI ...
了解更多信息,请使用this
答案 1 :(得分:1)
使用AsyncTask
。试试这个:
class YourAsyncTask extends AsyncTask<String, Void, RSSFeed> {
private Exception exception;
protected RSSFeed doInBackground(String... urls) {
try {
URL url= new URL(urls[0]);
SAXParserFactory factory =SAXParserFactory.newInstance();
SAXParser parser=factory.newSAXParser();
XMLReader xmlreader=parser.getXMLReader();
RssHandler theRSSHandler=new RssHandler();
xmlreader.setContentHandler(theRSSHandler);
InputSource is=new InputSource(url.openStream());
xmlreader.parse(is);
return theRSSHandler.getFeed();
} catch (Exception e) {
this.exception = e;
return null;
}
}
protected void onPostExecute(RSSFeed feed) {
// TODO: check this.exception
// TODO: do something with the feed
}
}
如何使用......
new YourAsyncTask().execute(urlToRssFeed);