I am developing a new application, and it needs to get a JSON object from my website in the url http://10.0.0.8/mybiren/json.php (I'm aware that it's a local ip, it's just for testing the app).
I've got the function that gets the content of the site, and it have worked on a regular Java project, but when I tried it on my Android aplication project it have failed. when I've debugged the app in order to find where it stops, it have stopped on the line that calls yc.getInputStream()
(yc is a URLConnection object). My friend said that I must add <uses-permission android:name="android.permission.INTERNET"/>
to my AndroidManifest.xml but it didn't help.
Stack Trace doesn't show anything special
Java code
package com.example.arghh_mybiren;
import android.support.v7.app.ActionBarActivity;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@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);
}
public void getJson(View v)
{
URL u;
try {
u = new URL("http://10.0.0.8/mybiren/json.php");
URLConnection yc = u.openConnection();
yc.setDoInput(true);
yc.setDoOutput(true);
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream())); //debugger crashed here
String inputLine;
StringBuilder a = new StringBuilder();
while ((inputLine = in.readLine()) != null)
a.append(inputLine);
in.close();
Toast.makeText(getApplicationContext(), a.toString(), Toast.LENGTH_SHORT).show();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
XML code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.arghh_mybiren.MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="getJson"
android:onClick="getJson" />
</RelativeLayout>
Stack Trace
11-04 19:59:26.452: I/PGA(17170): New SOCKET connection: com.example.mybiren (pid 17170, tid 17170)
11-04 19:59:26.452: W/PGA(17170): [17170] egl: eglCreateWindowSurface (0x5579a7a0, 0x0, 0x787ae3b8, 0x775840e0)
11-04 19:59:26.462: W/PGA(17170): [17170] egl: eglCreateWindowSurface (0x5579a7a0, 0x0, 0x787ae3b8, 0x775840e0) returned
11-04 19:59:26.472: D/OpenGLRenderer(17170): Enabling debug mode 0