Shut Down
شنبه 31 خرداد 1393, 20:18 عصر
سلام
من میخوام از طریق برنامه با سرور ارتباط برقرار کنم و ....
این کد
String url = "http://192.168.1.105/test/login.php";
ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("em", "a='" + eda.getText().toString() + "'"));
params.add(new BasicNameValuePair("pw", "b='" + edb.getText().toString() + "'"));
String result = Webservice.readUrl(url, params);
ولی وقتی این کد اجرا میشه این ارور میاد و به
String result = Webservice.readUrl(url, params);
و
HttpResponse response = client.execute(method);
در webService گیر میده
ارور :
06-21 22:58:55.207: E/AndroidRuntime(1682): java.lang.RuntimeException: Unable to start activity ComponentInfo{pr0gram3r72.Hogwarts/pr0gram3r72.Hogwarts.ActivityLogin}: android.os.NetworkOnMainThreadException
کد webservice
package pr0gram3r72.Hogwarts;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity ;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
public class Webservice {
public static String readUrl(String url, ArrayList<NameValuePair> params) {
try {
HttpClient client = new DefaultHttpClient();
HttpPost method = new HttpPost(url);
if (params != null) {
method.setEntity(new UrlEncodedFormEntity(params));
}
HttpResponse response = client.execute(method);
InputStream inputStream = response.getEntity().getContent();
String result = convertInputStreamToString(inputStream);
return result;
}
catch (ClientProtocolException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}
private static String convertInputStreamToString(InputStream inputStream) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder builder = new StringBuilder();
String line = "";
while ((line = reader.readLine()) != null) {
builder.append(line);
}
return builder.toString();
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
}
من میخوام از طریق برنامه با سرور ارتباط برقرار کنم و ....
این کد
String url = "http://192.168.1.105/test/login.php";
ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("em", "a='" + eda.getText().toString() + "'"));
params.add(new BasicNameValuePair("pw", "b='" + edb.getText().toString() + "'"));
String result = Webservice.readUrl(url, params);
ولی وقتی این کد اجرا میشه این ارور میاد و به
String result = Webservice.readUrl(url, params);
و
HttpResponse response = client.execute(method);
در webService گیر میده
ارور :
06-21 22:58:55.207: E/AndroidRuntime(1682): java.lang.RuntimeException: Unable to start activity ComponentInfo{pr0gram3r72.Hogwarts/pr0gram3r72.Hogwarts.ActivityLogin}: android.os.NetworkOnMainThreadException
کد webservice
package pr0gram3r72.Hogwarts;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity ;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
public class Webservice {
public static String readUrl(String url, ArrayList<NameValuePair> params) {
try {
HttpClient client = new DefaultHttpClient();
HttpPost method = new HttpPost(url);
if (params != null) {
method.setEntity(new UrlEncodedFormEntity(params));
}
HttpResponse response = client.execute(method);
InputStream inputStream = response.getEntity().getContent();
String result = convertInputStreamToString(inputStream);
return result;
}
catch (ClientProtocolException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}
private static String convertInputStreamToString(InputStream inputStream) {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder builder = new StringBuilder();
String line = "";
while ((line = reader.readLine()) != null) {
builder.append(line);
}
return builder.toString();
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
}