android get json登陆,android - How to get JSON from URL - Stack Overflow

I am getting Json file from url and want to show data from json file node by node and then to save the data into DATABASE. I am using the following code but not working please help me in finding error.I have mentioned the internet permission in manifest file. I am also adding my LOGCAT. thanks.

My Main Activity:

public class MainActivity extends Activity {

String url="maps.google.com/maps/api/geocode/json?latlng="+31.5279024+","+74.3495554;

JSONArray user = null;

private static final String TAG_USER = "results";

private static final String TAG_address = "address_components" ;

private static final String TAG_addressLName = "long_name";

private static final String TAG_addressSName= "short_name" ;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// Creating new JSON Parser

JsonParser jParser = new JsonParser();

// Getting JSON from URL

JSONObject json = jParser.getJSONFromUrl(url);

try {

// Getting JSON Array

user = json.getJSONArray(TAG_USER);

JSONObject c = user.getJSONObject(0);

// Storing JSON item in a Variable

String id = c.getString(TAG_address );

String name = c.getString(TAG_addressLName );

String email = c.getString( TAG_addressSName);

//Importing TextView

final TextView uid = (TextView)findViewById(R.id.uid);

final TextView name1 = (TextView)findViewById(R.id.name1);

final TextView email1 = (TextView)findViewById(R.id.email);

//Set JSON Data in TextView

uid.setText(id);

name1.setText(name);

email1.setText(email);

} catch (JSONException e) {

e.printStackTrace();

}

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

// Handle action bar item clicks here. The action bar will

// automatically handle clicks on the Home/Up button, so long

// as you specify a parent activity in AndroidManifest.xml.

int id = item.getItemId();

if (id == R.id.action_settings) {

return true;

}

return super.onOptionsItemSelected(item);

}

}

My JSON Class

package com.parsep.parse;

public class JsonParser {

static InputStream is = null;

static JSONObject jObj = null;

static String json = "";

public JsonParser (){

}

public JSONObject getJSONFromUrl(String url )

{

try

{

DefaultHttpClient httpClient= new DefaultHttpClient();

HttpGet httpPost = new HttpGet(url);

// httpPost.setEntity(new UrlEncodedFormEntity(params));

HttpResponse httpResponse = httpClient.execute(httpPost);

HttpEntity httpEntity = httpResponse.getEntity();

is = httpEntity.getContent();

}catch(UnsupportedEncodingException e)

{

e.printStackTrace();

}catch(ClientProtocolException e)

{

e.printStackTrace();

}catch(IOException e)

{

e.printStackTrace();

}

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 jObj;

}

}

My LOGCAT:

11-05 14:45:29.790: E/AndroidRuntime(533): FATAL EXCEPTION: main

11-05 14:45:29.790: E/AndroidRuntime(533): java.lang.RuntimeException: Unable to start activity

ComponentInfo{com.parsep.parse/com.parsep.parse.MainActivity}: java.lang.IllegalStateException:

Target host must not be null, or set in parameters. scheme=null, host=null, path=maps.google.com/maps/api/geocode/json

11-05 14:45:29.790: E/AndroidRuntime(533): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)

11-05 14:45:29.790: E/AndroidRuntime(533): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)

11-05 14:45:29.790: E/AndroidRuntime(533): at android.app.ActivityThread.access$600(ActivityThread.java:123)

11-05 14:45:29.790: E/AndroidRuntime(533): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)

11-05 14:45:29.790: E/AndroidRuntime(533): at android.os.Handler.dispatchMessage(Handler.java:99)

11-05 14:45:29.790: E/AndroidRuntime(533): at android.os.Looper.loop(Looper.java:137)

11-05 14:45:29.790: E/AndroidRuntime(533): at android.app.ActivityThread.main(ActivityThread.java:4424)

11-05 14:45:29.790: E/AndroidRuntime(533): at java.lang.reflect.Method.invokeNative(Native Method)

11-05 14:45:29.790: E/AndroidRuntime(533): at java.lang.reflect.Method.invoke(Method.java:511)

11-05 14:45:29.790: E/AndroidRuntime(533): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)

11-05 14:45:29.790: E/AndroidRuntime(533): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)

11-05 14:45:29.790: E/AndroidRuntime(533): at dalvik.system.NativeStart.main(Native Method)

11-05 14:45:29.790: E/AndroidRuntime(533): Caused by: java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=maps.google.com/maps/api/geocode/json

11-05 14:45:29.790: E/AndroidRuntime(533): at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:591)

11-05 14:45:29.790: E/AndroidRuntime(533): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:293)

11-05 14:45:29.790: E/AndroidRuntime(533): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)

11-05 14:45:29.790: E/AndroidRuntime(533): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)

11-05 14:45:29.790: E/AndroidRuntime(533): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)

11-05 14:45:29.790: E/AndroidRuntime(533): at com.parsep.parse.JsonParser.getJSONFromUrl(JsonParser.java:40)

11-05 14:45:29.790: E/AndroidRuntime(533): at com.parsep.parse.MainActivity.onCreate(MainActivity.java:29)

11-05 14:45:29.790: E/AndroidRuntime(533): at android.app.Activity.performCreate(Activity.java:4465)

11-05 14:45:29.790: E/AndroidRuntime(533): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)

11-05 14:45:29.790: E/AndroidRuntime(533): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)

11-05 14:45:29.790: E/AndroidRuntime(533): ... 11 more

11-05 14:45:30.010: I/dalvikvm(533): threadid=3: reacting to signal 3

11-05 14:45:30.049: I/dalvikvm(533): Wrote stack traces to '/data/anr/traces.txt'

11-05 14:45:30.350: I/dalvikvm(533): threadid=3: reacting to signal 3

11-05 14:45:30.360: I/dalvikvm(533): Wrote stack traces to '/data/anr/traces.txt'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值