错误:连接到WebService时
问题描述:
我写了一个程序并将它连接到mysql。 我写了一个Web服务类和使用HTTP客户端,但我有运行时错误, 请检查我的代码:错误:连接到WebService时
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class WebService {
public static String readUrl(String url) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
try {
HttpResponse httpResponse = httpClient.execute(httpPost);
InputStream inputStream = httpResponse.getEntity().getContent();
String result = convertInputStreamToString(inputStream);
return result;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private static String convertInputStreamToString(InputStream inputStream) {
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
String line = "";
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
return stringBuilder.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
String result = WebService.readUrl("http://localhost/note-server/");
if (result != null) {
try {
JSONArray tasks = new JSONArray(result);
for(int i=0;i<tasks.length();i++){
JSONObject task=tasks.getJSONObject(i);
Log.i("L",task.getString("task tilte"));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
,这里是错误代码,我有:
at com.example.parisa.noteapplication.WebService.read Url(WebService.java:26)
at com.example.parisa.noteapplication.MainActivity.on Create(MainActivity.java:47)
FATAL EXCEPTION: main
Process: com.example.parisa.noteapplication, PID: 29346
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.parisa.noteapplication/com.example.parisa.noteapplication.MainActivity}: android.os.NetworkOnMainThreadException
at android.app.ActivityThread.performLaunchActivity(A ctivityThread.java:2462)
和这里
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
String result = WebService.readUrl("http://localhost/note-server/");
if (result != null) {
try {
JSONArray tasks = new JSONArray(result);
for(int i=0;i<tasks.length();i++){
JSONObject task=tasks.getJSONObject(i);
Log.i("L",task.getString("task tilte"));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
答
你必须单独运行的线程上该readurl方法,
您无法将urlthread用于与webservices相关的事物。
在此先感谢
你得到一个NetworkOnMainThreadException参见[如何解决android.os.NetworkOnMainThreadException?](http://*.com/questions/6343166/how-to-fix-android-os- networkonmainthreadexception) – peko
在单独的线程中添加websevice。用户AsyncTask类的 –