بروز خطا
[message]
اشتراک در سوال
رای ها
[dataList]
مشکل بررسی داشتن اینترنت (ارسال و دریافت دیتا)
سلام دوستان من فرمی دارم که به سایت باید وصل بشه . نام کاربری و رمز را چک بکنه و نتیجه را برگردونه
حالا مشکل من اینه دفعه اول که روی دکمه میزنم Main Thread Exception میده و میگه اتصال برقرار نیست ولی بار دوم جواب میده
btnLogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
checkconnect();
}
});
private void checkconnect() {
try {
conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
mobile = conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
.getState();
wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
.getState();
if (mobile == NetworkInfo.State.CONNECTED
| mobile == NetworkInfo.State.CONNECTING) {
get_users();
} else if (wifi == NetworkInfo.State.CONNECTED
| wifi == NetworkInfo.State.CONNECTING) {
get_users();
} else {
G.selectMode = "offline";
toast.Redtoast(ActivityLogin.this, "Wifi or Gprs Disable");
}
} catch (Exception e) {
}
}
private void get_users() {
try {
String u, p;
u = edtusername_login.getText().toString();
p = edtpassword_login.getText().toString();
LoginUser loginUser = new LoginUser();
loginUser.GetUserData(ActivityLogin.this, u, p, user_error,
loginguser);
checkuserpass();
} catch (Exception e) {
Log.e(G.APP_LOG, "login error");
e.printStackTrace();
}
}
private void checkuserpass() {
if (G.userList.size() <= 0) {
toast.Redtoast(ActivityLogin.this, user_error);
} else {
G.GCode = G.userList.get(G.userList.size() - 1).getUs_GCode();
G.GAccCode = G.userList.get(G.userList.size() - 1).getUs_GAccCode();
toast.Bluetoast(ActivityLogin.this, "Get user pass ok");
Intent intent = new Intent(ActivityLogin.this, ActivityMain.class);
startActivity(intent);
ActivityLogin.this.finish();
}
}
تابعی که در کلاس LoginUser اجرا میشه
private void Checkdata() {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("uName", user));
params.add(new BasicNameValuePair("uPass", pass));
JSONObject json = jParser.makeHttpRequest(url_user, "POST", params);
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
UserStruct userStruct = new UserStruct();
String GCode = json.getString("GCode");
userStruct.setUs_GCode(GCode);
String GAccCode = json.getString("GAccCode");
userStruct.setUs_GAccCode(GAccCode);
G.userList.add(userStruct);
pDialog.dismiss();
}/*
* else { toast.Redtoast((Activity) context, user_error); }
*/
} catch (JSONException e) {
toast.Redtoast((Activity) context, user_error);
pDialog.dismiss();
}
}
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
public JSONParser() {
}
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
HttpClient httpClient;
HttpPost httpPost;
try {
// check for request method
if (method == "POST") {
httpClient = new DefaultHttpClient();
httpPost = new HttpPost(url);
if (params != null) {
httpPost.setEntity(new UrlEncodedFormEntity(params));
}
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} else if (method == "GET") {
httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
catch (ClientProtocolException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
try {
//BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
BufferedReader reader = new BufferedReader(new InputStreamReader(is), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
is.close();
json = sb.toString();
}
catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
try {
jObj = new JSONObject(json);
}
catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
return jObj;
}
}
ممنون میشم بگین چطوری جلوی دو بار کلیک را بگیرم برای ورود و با یک بار اطلاعات گرفته بشه و بررسی بشه
برای این سوال پاسخی وجود ندارد.
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .