2016-04-12 57 views
0
private EditText name,email,password,phone; 
    private Button reg_btn; 
    private ProgressDialog pDialog; 

    JSONParser jsonParser = new JSONParser(); 
    private static final String LOGIN_URL = "http://vichomes.softcorp.ca/android/register/"; 
    private static final String TAG_SUCCESS = "success"; 
    private static final String TAG_MESSAGE = "message"; 

這個我signup.java類。我發送用戶憑據到服務器錯誤解析數據org.json.JSONException值類型爲java.lang.String <html>

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.sign_up); 


     name=(EditText)findViewById(R.id.name); 
     email=(EditText)findViewById(R.id.email1); 
     password=(EditText)findViewById(R.id.password1); 
     phone=(EditText)findViewById(R.id.phone); 
     reg_btn=(Button)findViewById(R.id.button3); 

     reg_btn.setOnClickListener(this); 


    } 


    public void onClick(View v) { 
     // TODO Auto-generated method stub 

     new CreateUser().execute(); 

     } 

    class CreateUser extends AsyncTask<String, String, String> { 

     boolean failure = false; 
     String namestr = name.getText().toString(); 
     String emailstr = email.getText().toString(); 
     String passwordstr = password.getText().toString(); 
     String phonestr= phone.getText().toString(); 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(sign_up.this); 
      pDialog.setMessage("Successfull"); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(true); 
      pDialog.show(); 
     } 

這裏的TO JSON

轉換
 @Override 
     protected String doInBackground(String... args){ 

      // TODO Auto-generated method stub 
      // Check for success tag 
      int success; 


      try { 
       // Building Parameters 
       List<NameValuePair> params = new ArrayList<NameValuePair>(); 
       params.add(new BasicNameValuePair("name", namestr)); 
       params.add(new BasicNameValuePair("email", emailstr)); 
       params.add(new BasicNameValuePair("password",passwordstr)); 
       params.add(new BasicNameValuePair("phone", phonestr)); 

       Log.d("request!", "starting"); 

       //Posting user data to script 
       JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST", params); 

打印完整JSON響應

   Log.d("sign up attempt", json.toString() 

JSON的成功元素報名屏幕

   success = json.getInt(TAG_SUCCESS); 
       if (success == 1) { 
        Log.d("User Created!", json.toString()); 
        Intent i= new Intent(sign_up.this,donation.class); 
        startActivity(i); 
        return json.getString(TAG_MESSAGE); 
       } 
else{ 
        Log.d("Login Failure!", json.getString(TAG_MESSAGE)); 
        return json.getString(TAG_MESSAGE); 

       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

      return null; 


     } 

     protected void onPostExecute(String file_url) { 
      // dismiss the dialog once product deleted 
      pDialog.dismiss(); 
      if (file_url != null){ 
       Toast.makeText(sign_up.this, file_url, Toast.LENGTH_LONG).show(); 
      } 

     } 
    } 




    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
// Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if(id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

} 

XML代碼。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#1F874E" 
    android:orientation="vertical"> 

<TextView 
    android:layout_width="221dp" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:text="Sign Up" 
    android:id="@+id/textView2" 
    android:textColor="#FFFFFF" 
    android:textStyle="bold" 
    android:textSize="40px" 
    android:layout_marginLeft="20dp" 
    android:layout_marginTop="10dp"/> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Name" 
    android:id="@+id/textView3" 
    android:layout_marginTop="20dp" 
    android:layout_marginLeft="20dp" 
    android:textSize="20dp" 
    android:textStyle="bold"/> 

<EditText 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/name" 
    android:layout_gravity="center_horizontal" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Email" 
    android:id="@+id/textView4" 
    android:layout_marginTop="20dp" 
    android:layout_marginLeft="20dp" 
    android:textSize="20dp" 
    android:textStyle="bold"/> 

<EditText 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:inputType="textEmailAddress" 
    android:ems="10" 
    android:id="@+id/email1" 
    android:layout_gravity="center_horizontal" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Password" 
    android:id="@+id/password5" 
    android:layout_marginTop="20dp" 
    android:layout_marginLeft="20dp" 
    android:textSize="20dp" 
    android:textStyle="bold"/> 

<EditText 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:inputType="textPassword" 
    android:ems="10" 
    android:id="@+id/password1" /> 


    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Confirm Password" 
     android:id="@+id/textView" 
     android:layout_marginTop="20dp" 
     android:layout_marginLeft="20dp" 
     android:textSize="20dp" 
     android:textStyle="bold"/> 
    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="textPassword" 
     android:ems="10" 
     android:id="@+id/password2" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Phone" 
     android:id="@+id/textView16" 
     android:layout_marginTop="20dp" 
     android:layout_marginLeft="20dp" 
     android:textSize="20dp" 
     android:textStyle="bold"/> 
    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="text" 
     android:ems="10" 
     android:id="@+id/phone" /> 

    <Button 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Submit" 
    android:id="@+id/button3" 
    android:layout_gravity="center_horizontal" 
    android:layout_marginTop="40dp" 
    android:layout_marginLeft="0dp" 
    android:textSize="20dp" 
    android:textStyle="bold" 
     android:onClick="Submit"/> 

</LinearLayout> 

異常日誌的

Error parsing data org.json.JSONException: Value <html> of type java.lang.String cannot be converted to JSONObject 
    04-12 10:00:08.302 6397-6478/com.virasatsolutions.www.donapp E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #3 
                        Process: com.virasatsolutions.www.donapp, PID: 6397 
                        java.lang.RuntimeException: An error occurred while executing doInBackground() 
                         at android.os.AsyncTask$3.done(AsyncTask.java:309) 
                         at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354) 
                         at java.util.concurrent.FutureTask.setException(FutureTask.java:223) 
                         at java.util.concurrent.FutureTask.run(FutureTask.java:242) 
                         at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
                         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
                         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
                         at java.lang.Thread.run(Thread.java:818) 
                        Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.toString()' on a null object reference 
                         at com.virasatsolutions.www.donapp.sign_up$CreateUser.doInBackground(sign_up.java:169) 
                         at com.virasatsolutions.www.donapp.sign_up$CreateUser.doInBackground(sign_up.java:129) 
                         at android.os.AsyncTask$2.call(AsyncTask.java:295) 
                         at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
                         at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)  
                         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)  
                         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)  
                         at java.lang.Thread.run(Thread.java:818)  
    04-12 10:00:08.437 6397-6422/com.virasatsolutions.www.donapp W/EGL_emulation: eglSurfaceAttrib not implemented 
    04-12 10:00:08.438 6397-6422/com.virasatsolutions.www.donapp W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xac43fe00, error=EGL_SUCCESS 
    04-12 10:00:08.530 6397-6422/com.virasatsolutions.www.donapp E/Surface: getSlotFromBufferLocked: unknown buffer: 0xae6f5af0 
    04-12 10:00:08.542 6397-6422/com.virasatsolutions.www.donapp E/Surface: getSlotFromBufferLocked: unknown buffer: 0xae6f5460 
    04-12 10:00:08.783 6397-6397/com.virasatsolutions.www.donapp E/WindowManager: android.view.WindowLeaked: Activity com.virasatsolutions.www.donapp.sign_up has leaked window com.android.internal.policy.PhoneWindow$DecorView{954561b V.E...... R......D 0,0-1026,348} that was originally added here 
                         at android.view.ViewRootImpl.<init>(ViewRootImpl.java:368) 
                         at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:299) 
                         at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85) 
                         at android.app.Dialog.show(Dialog.java:319) 
                         at com.virasatsolutions.www.donapp.sign_up$CreateUser.onPreExecute(sign_up.java:144) 
                         at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:604) 
                         at android.os.AsyncTask.execute(AsyncTask.java:551) 
                         at com.virasatsolutions.www.donapp.sign_up.onClick(sign_up.java:125) 
                         at android.view.View.performClick(View.java:5198) 
                         at android.view.View$PerformClick.run(View.java:21147) 
                         at android.os.Handler.handleCallback(Handler.java:739) 
                         at android.os.Handler.dispatchMessage(Handler.java:95) 
                         at android.os.Looper.loop(Looper.java:148) 
                         at android.app.ActivityThread.main(ActivityThread.java:5417) 
                         at java.lang.reflect.Method.invoke(Native Method) 
                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
+0

API正在返回HTML內容而不是JSON內容。 –

回答

0

你的API中的問題,它返回的HTML內容。

要測試它並找出問題,請使用Postman或任何其他Restful客戶端發送請求並獲取響應。

1

錯誤你有..

Value <html> of type java.lang.String cannot be converted to JSONObject 

好像你api服務不反應良好。實際上你的api有一些問題。

如果任何REst-api一些html標記值就意味着返回或者一些exceptionservice提出,由於一些input mismmatch。

我不能告訴你確切的原因,但有幾個更多的原因背後。

我認爲您應該在api網址中收到的String變量中添加一個支票。

String urlResponse = "{}" // suppose this variable holds the response from the URL. 

//then put a check to this variable such that 
if(urlResponse.startsWith("{")){ 

    //in this block you may parse your response from String to JSONObject. 
    JSONObject v_obj = (JSONObject) new parser().parse(urlResponse); 
}else if(urlResponse.startsWith("[")){ 
    //in this block you may parse your response from String to JSONArray. 
    JSONArray v_obj = (JSONArray) new parser().parse(urlResponse); 
    }else{ 
     //write if you don't receive correct response. 
    } 

注意: - 如果urlResponse.startsWith("{")回報true那麼它是一個JSONObject類型。如果urlResponse.startsWith("[")返回true那麼它包含JSONArray

謝謝..

相關問題