我正在开发一个新的应用程序,它需要从我的网站http://10.0.0.8/mybiren/json.php中获取一个JSON对象(我知道它是本地IP,它是' s只是为了测试应用程序)
我已经获得了获取网站内容的功能,并且它已经在常规Java项目上工作,但是当我在我的Android应用程序项目上尝试它时,它已经失败了。当我调试应用程序以找到它停止的位置时,它已停止在调用yc.getInputStream()
的行上(yc是URLConnection对象)。我的朋友说我必须将<uses-permission android:name="android.permission.INTERNET"/>
添加到我的AndroidManifest.xml中,但它没有帮助。
Stack Trace并没有显示任何特别的东西
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();
}
}
}
<小时/>
<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>
<小时/>
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
答案 0 :(得分:1)
我真的不知道出了什么问题,但是我的朋友成功地帮助了我并向我发送了以下代码(对我有用):
package com.example.arghh_mybiren;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private class MyTask extends AsyncTask<String, Void, String>
{
@Override
protected String doInBackground(String ... urls)
{
try
{
URL url = new URL(urls[0]);
URLConnection uc = url.openConnection();
//String j = (String) uc.getContent();
uc.setDoInput(true);
BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
String inputLine;
StringBuilder a = new StringBuilder();
while ((inputLine = in.readLine()) != null)
a.append(inputLine);
in.close();
return a.toString();
}
catch (Exception e)
{
return e.getMessage();
}
}
@Override
protected void onPostExecute(String result)
{
Toast.makeText(getApplication(), result, Toast.LENGTH_LONG).show();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button loadBtn = (Button) this.findViewById(R.id.btn_load);
loadBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Load();
}
});
}
public void Load()
{
Toast.makeText(this, "Button Clicked", Toast.LENGTH_SHORT).show();
MyTask taskLoad = new MyTask();
taskLoad.execute("http://10.0.0.8/mybiren/JSON.php?day=Monday");
}
@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);
}
}
<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/btn_load"
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>
感谢所有试图帮助我的人。