我正在开发一个Android应用程序。在这里,我想通过发送语言环境来获取国家名称意味着当地语言。任何人都可以告诉我问题出在哪里?我无法获取值并在微调器中显示。 我这样做了: -
public class Main2Activity extends Activity {
final Context context = this;
public String readJSONFeed(String URL) {
StringBuilder stringBuilder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(URL);
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}} else {
Log.e("JSON", "Failed to download file");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return stringBuilder.toString();
}
private class ReadJSONFeedTask extends AsyncTask<String, Void, String> {
protected String doInBackground(String... urls) {
return readJSONFeed(urls[0]);
}
protected void onPostExecute(String result) {
Spinner mySpinner = (Spinner) findViewById(R.id.spinner1);
try {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line;
while((line=in.readLine())!=null){
JSONArray jsonArray = new JSONArray(line);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
List<String> list = new ArrayList<String>();list.add(jsonObject.getString("name"));
mySpinner
.setAdapter(new ArrayAdapter<String>(
Main2Activity.this,
android.R.layout.simple_spinner_dropdown_item,
list));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url = context.getResources().getConfiguration().locale
.getLanguage();
url = "http://Its my server address?locale=" + url.toUpperCase();
Log.e("Web call", url);
new ReadJSONFeedTask().execute(url);
}
}
请帮帮我。
答案 0 :(得分:1)
这里的问题是onPostExecute方法中的while循环。哪个阻止UI线程并将导致ANR。你是从System.in读什么的? 根据逻辑,我了解所有国家名称在“结果”对象中可用。检查您在结果对象中获得的内容。 您正在向结果添加值但不使用它。这意味着doInBackground中的逻辑是无用的。我认为你试图通过从输入strem中读取行来从结果对象中分离行。您可以将每行存储在ArrayList中并使用它,而不是这样。以下是可能的解决方案。希望这有助于!
public class Main2Activity extends Activity {
final Context context = this;
public ArrayList<String> readJSONFeed(String URL) {
// Add all lines to this list.
ArrayList<String> data = new ArrayList<String>();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(URL);
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
data.add(line);
}
} else {
Log.e("JSON", "Failed to download file");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
private class ReadJSONFeedTask extends
AsyncTask<String, Void, ArrayList<String>> {
protected ArrayList<String> doInBackground(String... urls) {
return readJSONFeed(urls[0]);
}
protected void onPostExecute(ArrayList<String> result) {
Spinner mySpinner = (Spinner) findViewById(R.id.spinner1);
try {
// Parse each line and add "name" to this list.
List<String> nameList = new ArrayList<String>();
for (String line : result) {
JSONArray jsonArray = new JSONArray(line);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
nameList.add(jsonObject.getString("name"));
}
}
// Once complete traversing is done, set Adapter
mySpinner.setAdapter(new ArrayAdapter<String>(
Main2Activity.this,
android.R.layout.simple_spinner_dropdown_item,
nameList));
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url = context.getResources().getConfiguration().locale
.getLanguage();
url = "http://Its my server address?locale=" + url.toUpperCase();
Log.e("Web call", url);
new ReadJSONFeedTask().execute(url);
}
}