问题
I am newbie to android development. I am using android studio for my application. I have created a DB and then created a web service in Yii, tested it on ARC and use the GET method and it shows the result perfectly. After that i created an app in which i want to use the web service and use it's get method to display a result.
Below is my DB table structure
The scenario is when ever a user enters the id in the app and hit the submit button it should show the corresponding username. For this see the below codes
Manifest
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
MainActivity.java
public class MainActivity extends AppCompatActivity {
String URLGET = "http://my_ip:8000/app/web/users/";
String result = "";
TextView tv;
public static final String LOG_TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = (Button) findViewById(R.id.btn_Submit);
button.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
String reqURL = URLGET;
new RestOperation().execute(reqURL);
//callWebService(query);
}
});
}
public class RestOperation extends AsyncTask<String, Void, Void> {
final HttpClient httpClient = new DefaultHttpClient();
String content;
String error;
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
String data = "";
TextView getData = (TextView) findViewById(R.id.getData);
EditText userInput = (EditText) findViewById(R.id.EnterId);
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog.setTitle("Please Wait...");
progressDialog.show();
try {
data += "&" + URLEncoder.encode("data", "UTF-8") + "-" + userInput.getText();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
@Override
protected Void doInBackground(String... params) {
BufferedReader br = null;
URL url = null;
try {
url = new URL(params[0]);
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(connection.getOutputStream());
outputStreamWriter.write(data);
outputStreamWriter.flush();
br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line);
sb.append(System.getProperty("line.separator"));
}
content = sb.toString();
} catch (MalformedURLException e) {
error = e.getMessage();
e.printStackTrace();
} catch (IOException e) {
error = e.getMessage();
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
progressDialog.dismiss();
if (error != null) {
getData.setText("Error" + error);
} else {
getData.setText(content);
}
}
}
}
But when i run the app, enter any given id's and perform submit. The progress bat shows up for a long while and then the app crashes giving the following errors.
FATAL EXCEPTION: AsyncTask #1
Process: com.example.accurat.webservice, PID: 642
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:304)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.BufferedReader.close()' on a null object reference
at com.example.accurat.webservice.MainActivity$RestOperation.doInBackground(MainActivity.java:141)
at com.example.accurat.webservice.MainActivity$RestOperation.doInBackground(MainActivity.java:72)
at android.os.AsyncTask$2.call(AsyncTask.java:292)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
02-07 15:57:42.929 642-642/com.example.accurat.webservice E/WindowManager: android.view.WindowLeaked: Activity com.example.accurat.webservice.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{3f2c8572 V.E..... R......D 0,0-729,322} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:363)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:271)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
at android.app.Dialog.show(Dialog.java:298)
at com.example.accurat.webservice.MainActivity$RestOperation.onPreExecute(MainActivity.java:90)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:591)
at android.os.AsyncTask.execute(AsyncTask.java:539)
at com.example.accurat.webservice.MainActivity$1.onClick(MainActivity.java:65)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
I searched many articles on google and couldn't find the correct answers, though i tried to implement them but still no result.
Below are the findings from my search.
- link1
- link2
link3
link 4
Update 1
By using Ben and X3Btel suggestions my app stops crashing but it's showing me below error on emulator
I then again replaced my ip to localhost but still saw the same error.
I am testing my app on the emulator so i put my ip address in the url.
I am stuck to from almost all the day. Any help would be appreciated.
回答1:
You are getting a nullpointer exception.
if exception happened before initializing the reader it will lead to this error because br.close() is placed in finally block. It's good practice to always check for null before closing resources in finally.
try this in finally block
if(br!= null)
br.close();
Good luck
来源:https://stackoverflow.com/questions/42088579/progress-dialog-keeps-on-displaying-in-android-studio