我正在尝试将用户在应用程序页面中输入的值发布到在000webhost服务器中创建的数据库中,但是在Android Studio中显示了一些错误。
这是我的java文件,
IndividualUserFragment.java:
package fragmentsstartup.hh.safmical;
import android.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Iterator;
import javax.net.ssl.HttpsURLConnection;
import safmical.h.R;
/**
* Created by hadvani on 4/16/2017.
*/
public class IndividualUserFragment extends Fragment {
EditText name;
EditText adhar;
EditText email;
EditText password;
EditText contact;
Button bt;
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View rootview= inflater.inflate(R.layout.fragment_individualuser,container,false);
bt = (Button) rootview.findViewById(R.id.button2);
name= (EditText) rootview.findViewById(R.id.editText2);
adhar= (EditText) rootview.findViewById(R.id.editText3);
email= (EditText) rootview.findViewById(R.id.editText4);
password= (EditText) rootview.findViewById(R.id.editText5);
contact= (EditText) rootview.findViewById(R.id.editText6);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// AsyncTask obj = new AsyncDemo();
// obj.execute();
new myasyncDemo().execute();
}
});
return rootview;
}
}
class myasyncDemo extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
try {
URL url = new URL("***************"); // here is my URL path
JSONObject postDataParams = new JSONObject();
postDataParams.put("name", name.getText().toString());//here it shows suggestion on name field that "can not resolve symbol name"
postDataParams.put("adhar", adhar.getText().toString());//here also same "can not resolve symbol adhar"
postDataParams.put("email", email.getText().toString());//here also same "can not resolve symbol email "
postDataParams.put("password", password.getText().toString());//here also same "can not resolve symbol password "
postDataParams.put("contact", contact.getText().toString());//here also same "can not resolve symbol contact"
Log.e("params", postDataParams.toString());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode = conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(
new InputStreamReader(
conn.getInputStream()));
StringBuffer sb = new StringBuffer("");
String line = "";
while ((line = in.readLine()) != null) {
sb.append(line);
break;
}
in.close();
return sb.toString();
} else {
return new String("false : " + responseCode);
}
} catch (Exception e) {
return new String("Exception: " + e.getMessage());
}
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
public String getPostDataString(JSONObject params) throws Exception {
StringBuilder result = new StringBuilder();
boolean first = true;
Iterator<String> itr = params.keys();
while (itr.hasNext()) {
String key = itr.next();
Object value = params.get(key);
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(key, "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(value.toString(), "UTF-8"));
}
return result.toString();
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Log.e("DATA", result);
}
}
}
我的xml文件:
fragment_individualuser.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e6e6fa">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button2"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:text="save"
android:textSize="20dp"
android:onClick="save"
android:layout_below="@+id/editText6"
android:layout_alignLeft="@+id/editText6"
android:layout_alignStart="@+id/editText6" />
<EditText
android:id="@+id/editText6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="Contact"
android:layout_marginTop="20dp"
android:layout_below="@+id/editText5" android:background="#ffffff"
android:layout_alignLeft="@+id/editText5"
android:layout_alignStart="@+id/editText5"
android:layout_alignRight="@+id/editText4"
android:layout_alignEnd="@+id/editText4" />
<EditText
android:id="@+id/editText5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" android:background="#ffffff"
android:inputType="textPersonName"
android:layout_marginTop="20dp"
android:hint="Create Password(6 to 10 characters)"
android:layout_below="@+id/editText4"
android:layout_alignLeft="@+id/editText4"
android:layout_alignStart="@+id/editText4"
android:layout_alignRight="@+id/editText4"
android:layout_alignEnd="@+id/editText4" />
<EditText
android:id="@+id/editText4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="Email Address" android:background="#ffffff"
android:layout_marginTop="20dp"
android:layout_below="@+id/editText3"
android:layout_alignLeft="@+id/editText3"
android:layout_alignStart="@+id/editText3"
android:layout_alignRight="@+id/textView2"
android:layout_alignEnd="@+id/textView2" />
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" android:background="#ffffff"
android:hint="Adhar Number"
android:layout_marginTop="20dp"
android:layout_below="@+id/editText2"
android:layout_alignLeft="@+id/editText2"
android:layout_alignStart="@+id/editText2"
android:layout_alignRight="@+id/textView2"
android:layout_alignEnd="@+id/textView2" />
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="You are registering as a individual user."
android:textColor="#000"
android:textSize="25dp"
android:layout_below="@+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="Create Account"
android:textColor="#000"
android:textSize="45dp" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/textView3"
android:layout_marginLeft="19dp"
android:layout_marginStart="19dp"
android:layout_marginTop="14dp"
android:background="#ffffff"
android:ems="10"
android:inputType="textPersonName"
android:hint="Enter Your Name"
android:layout_alignRight="@+id/textView2"
android:layout_alignEnd="@+id/textView2" />
<Button
android:id="@+id/button"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="cancel"
android:onClick="cancel"
android:textSize="18dp"
android:layout_alignBaseline="@+id/button2"
android:layout_alignBottom="@+id/button2"
android:layout_alignRight="@+id/editText6"
android:layout_alignEnd="@+id/editText6" />
</RelativeLayout>
</ScrollView>
</FrameLayout>
当我运行它时,它显示此错误
Error:(168, 1) error: class, interface, or enum expected
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
并在myasyncDemo类的try块中的java文件中显示建议/错误,这是我在其注释行中指定的。
请帮我解决这个问题。
最佳答案
1.清理项目。
2.重建项目。
3.使缓存无效并重新启动android studio