My application work in emulator BUT in my decive no.

  • Replies:2
INTENTer
  • Forum posts: 1

Feb 12, 2014, 11:48:35 PM via Website

This is my code of Login.java:
1package com.example.***;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import org.apache.http.NameValuePair;
7import org.apache.http.message.BasicNameValuePair;
8import org.json.JSONArray;
9import org.json.JSONException;
10import org.json.JSONObject;
11
12
13import android.os.AsyncTask;
14import android.os.Bundle;
15import android.app.Activity;
16import android.app.ProgressDialog;
17import android.content.Intent;
18import android.util.Log;
19import android.view.View;
20import android.widget.EditText;
21import android.widget.TextView;
22import android.widget.Toast;
23
24public class Login extends Activity {
25
26
27 // Progress Dialog
28 private ProgressDialog pDialog3;
29
30 // JSON parser class
31 JSONParser jsonParser = new JSONParser();
32
33 // single product url
34
35 private static final String url_product_detials = " My Url ";
36 //private static final String TAG_PID = "id";
37 // JSON Node names
38 private static final String TAG_SUCCESS = "success";
39
40 EditText etUsername,etPassword;
41 TextView tvUsername,tvPassword, tvError;
42 @Override
43 public void onCreate(Bundle savedInstanceState) {
44 super.onCreate(savedInstanceState);
45 setContentView(R.layout.activity_login);
46
47 etUsername = (EditText) findViewById(R.id.etUsername);
48 etPassword = (EditText) findViewById(R.id.etPassword);
49
50 }
51
52 public void goButtonClicked(View v) {
53 boolean chUser = true,chPass = true;
54 String toastpro = "Problem With ";
55 if(etUsername.getText().toString().equals(""))
56 {
57 chUser = false;
58 }
59 if(etPassword.getText().toString().equals(""))
60 {
61 chPass = false;
62 }
63
64
65 // אחרי שעבר את כל הבדיקות
66
67 if(chUser != false && chPass != false)
68 {
69
70 new GetLogin().execute();
71
72 //Intent i = new Intent(this,MyProfile.class);
73 //startActivity(i);
74 }
75 else
76 {
77 if(chUser == false)
78 {
79 toastpro += "Username";
80 }
81 if(chPass == false)
82 {
83 toastpro += ",Password";
84 }
85 Toast.makeText(this, toastpro, Toast.LENGTH_SHORT).show();
86 }
87 }
88
89 public void goSignUp(View v) {
90 Intent i2 = new Intent(this,SignUp.class);
91 startActivity(i2);
92 }
93
94
95/**
96 * Background Async Task to Get complete product details
97 * */
98class GetLogin extends AsyncTask<String, String, String> {
99
100
101
102 @Override
103 protected void onPreExecute() {
104 Log.e("onPreExecute","dialog");
105 super.onPreExecute();
106 pDialog3 = new ProgressDialog(Login.this);
107 pDialog3.setMessage("Loading Please wait...");
108 pDialog3.setIndeterminate(false);
109 pDialog3.setCancelable(true);
110 pDialog3.show();
111 }
112
113 /**
114 * Getting product details in background thread
115 * */
116
117 protected String doInBackground(String... params) {
118 Log.e("doInBackground","runOnUiThread");
119 // updating UI from Background Thread
120 runOnUiThread(new Runnable() {
121 public void run() {
122 Log.e("runing","runing");
123 // Check for success tag
124 tvError = (TextView) findViewById(R.id.tvError);
125 tvUsername = (TextView) findViewById(R.id.tvUsername);
126
127 String username = etUsername.getText().toString();
128 etUsername = (EditText) findViewById(R.id.etUsername);
129 etPassword = (EditText) findViewById(R.id.etPassword);
130
131
132 tvUsername.setText(username);
133 int success;
134 try {
135
136 // Building Parameters
137 List<NameValuePair> params = new ArrayList<NameValuePair>();
138 params.add(new BasicNameValuePair("username", username));
139
140 // getting product details by making HTTP request
141 // Note that product details url will use GET request
142 JSONObject json = jsonParser.makeHttpRequest(
143 url_product_detials, "GET", params);
144
145 Log.d("Single Product Details", url_product_detials);
146 // check your log for json response
147 Log.d("Single Product Details", json.toString());
148
149 // json success tag
150 success = json.getInt(TAG_SUCCESS);
151 if (success == 1) {
152 Log.e("doInBackground","success login");
153 // successfully received product details
154 JSONArray productObj = json
155 .getJSONArray("user"); // JSON Array
156
157 // get first product object from JSON Array
158 JSONObject product = productObj.getJSONObject(0);
159
160 // product with this pid found
161 // Edit Text
162
163 String passwordact = etPassword.getText().toString();
164 // display product data in EditText
165 ///////////////////////////////////////////////////////////////////////////////////////בעיה עם האימות סיסמה
166 String password = product.getString("password");
167 String id = "Mor";
168 if(password.equals(passwordact))
169 {
170 id = product.getString("id");
171 Log.e("doInBackground","un intent login");
172 Intent i2 = new Intent(Login.this,Menu2.class);
173 i2.putExtra("image", id);
174 startActivity(i2);
175 finish();
176
177
178 }
179 else
180 {
181 tvError.setText("Problem With The PASSWORD in server");
182 }
183 Log.e("doInBackground","set now");
184
185
186
187
188
189 Log.e("doInBackground","after set text");
190
191 }else{
192
193 tvError.setText("Problem With The USERNAME in server");
194
195 }
196 } catch (JSONException e) {
197 e.printStackTrace();
198 }
199 }
200
201 });
202
203 return null;
204 }
205 protected void onPostExecute(String file_url) {
206 // dismiss the dialog once got all details
207 pDialog3.dismiss();
208 }
209}
210}

and this is the logs problem:
102-13 00:19:39.115: E/SensorManager(19561): thread start
202-13 00:19:39.315: E/SpannableStringBuilder(19561): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
302-13 00:19:39.315: E/SpannableStringBuilder(19561): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
402-13 00:19:42.760: E/SpannableStringBuilder(19561): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
502-13 00:19:42.760: E/SpannableStringBuilder(19561): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
602-13 00:19:49.060: E/onPreExecute(19561): dialog
702-13 00:19:49.120: E/doInBackground(19561): runOnUiThread
802-13 00:19:49.170: E/runing(19561): runing
902-13 00:19:49.195: E/AndroidRuntime(19561): FATAL EXCEPTION: main
1002-13 00:19:49.195: E/AndroidRuntime(19561): android.os.NetworkOnMainThreadException
1102-13 00:19:49.195: E/AndroidRuntime(19561): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
1202-13 00:19:49.195: E/AndroidRuntime(19561): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
1302-13 00:19:49.195: E/AndroidRuntime(19561): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
1402-13 00:19:49.195: E/AndroidRuntime(19561): at java.net.InetAddress.getAllByName(InetAddress.java:214)
1502-13 00:19:49.195: E/AndroidRuntime(19561): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
1602-13 00:19:49.195: E/AndroidRuntime(19561): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
1702-13 00:19:49.195: E/AndroidRuntime(19561): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
1802-13 00:19:49.195: E/AndroidRuntime(19561): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
1902-13 00:19:49.195: E/AndroidRuntime(19561): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:670)
2002-13 00:19:49.195: E/AndroidRuntime(19561): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:509)
2102-13 00:19:49.195: E/AndroidRuntime(19561): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
2202-13 00:19:49.195: E/AndroidRuntime(19561): at com.example.hotornot.JSONParser.makeHttpRequest(JSONParser.java:60)
2302-13 00:19:49.195: E/AndroidRuntime(19561): at com.example.hotornot.Login$GetLogin$1.run(Login.java:142)
2402-13 00:19:49.195: E/AndroidRuntime(19561): at android.os.Handler.handleCallback(Handler.java:615)
2502-13 00:19:49.195: E/AndroidRuntime(19561): at android.os.Handler.dispatchMessage(Handler.java:92)
2602-13 00:19:49.195: E/AndroidRuntime(19561): at android.os.Looper.loop(Looper.java:137)
2702-13 00:19:49.195: E/AndroidRuntime(19561): at android.app.ActivityThread.main(ActivityThread.java:4898)
2802-13 00:19:49.195: E/AndroidRuntime(19561): at java.lang.reflect.Method.invokeNative(Native Method)
2902-13 00:19:49.195: E/AndroidRuntime(19561): at java.lang.reflect.Method.invoke(Method.java:511)
3002-13 00:19:49.195: E/AndroidRuntime(19561): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
3102-13 00:19:49.195: E/AndroidRuntime(19561): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
3202-13 00:19:49.195: E/AndroidRuntime(19561): at dalvik.system.NativeStart.main(Native Method)

— modified on Feb 12, 2014, 11:49:01 PM

Reply
KrayZ Logic
  • Forum posts: 17

Feb 13, 2014, 5:02:29 PM via Website

It looks like on line 142,

JSONObject json = jsonParser.makeHttpRequest(143 url_product_detials, "GET", params);

needs to be done on a seperate thread from the UI thread. Have you tried doing it as an AsyncTask?

Reply
Pascal P.
  • Admin
  • Forum posts: 11,286

Feb 19, 2014, 5:30:22 PM via Website

LogCat says "android.os.NetworkOnMainThreadException" that means you make network Operations like http get in android main thread, to solve this look at AsyncTask.

— modified on Feb 19, 2014, 5:30:31 PM

LG Pascal //It's not a bug, it's a feature. :) ;)

Reply