2017-05-04 80 views
1

我尝试后,用户在我的应用程序页面进入到000webhost的创建我的数据库中的值server.But它显示在Android的一些错误工作室。“”无法解析符号'错误的AsyncTask代码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文件建议/错误。

请帮我解决这个问题。

+0

清理和重建,你很好走 –

+0

不working.it给我同样的错误,当我尝试重建它。 – harsh

+0

需要更多信息,不能帮助它。它说,你在(168,1) –

回答

2

1.Clean项目。 2.重建项目。 3.无效缓存并重新启动android studio

+0

不working.it给人同样的错误,当我米试图重建它。 – harsh

+0

好吧尝试其他方式,我认为这是值得与缺少括号,括号或开始认为这种方法定义之前关闭类主体,请检查。 @harsh –

相关问题