I am developing an android app.Here,I want to fetch the country name by sending locale means local language.Can anyone tell me where is the problem? I can not fetch the value and displays in spinner. I have done this:-
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);
}
}
Please help me.