2016-08-17 5 views
2

Ich bin sehr neu in Android und ich würde gerne einige Variablen aus meiner Android-Anwendung auf eine PHP-Seite übergeben. Ich habe einen Code von einer Seite verwendet und versucht, ihn an das anzupassen, was ich brauche, aber im Moment werden die Variablen nicht übergeben. Was ich machen möchte ist, dass der Button mit der ID "button_wp" die Funktion "run" aktiviert. Der Code, den ich verwende, ist die folgende:Objekt an php senden von Android

Android xml

<Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Go to webpage" 
     android:id="@+id/button_wp" 
     android:onClick="wp" /> 

Android Java

public class Main3Activity extends AppCompatActivity { 

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

    } 

public void wp(View v) { 

     new Thread(new Runnable() { 
      @Override 
      public void run() { 
       OutputStream os = null; 
       InputStream is = null; 
       HttpURLConnection conn = null; 
       try { 
        //constants 
        URL url = new URL("https://www.mypage.com/login_app.php"); 
        JSONObject jsonObject = new JSONObject(); 
        jsonObject.put("precio", "5000€"); 
        String message = jsonObject.toString(); 

        conn = (HttpURLConnection) url.openConnection(); 
        conn.setReadTimeout(10000 /*milliseconds*/); 
        conn.setConnectTimeout(15000 /* milliseconds */); 
        conn.setRequestMethod("POST"); 
        conn.setDoInput(true); 
        conn.setDoOutput(true); 
        conn.setFixedLengthStreamingMode(message.getBytes().length); 

        //make some HTTP header nicety 
        conn.setRequestProperty("Content-Type", "application/json;charset=utf-8"); 
        conn.setRequestProperty("X-Requested-With", "XMLHttpRequest"); 

        //open 
        conn.connect(); 

        //setup send 
        os = new BufferedOutputStream(conn.getOutputStream()); 
        os.write(message.getBytes()); 
        //clean up 
        os.flush(); 

        //do somehting with response 
        is = conn.getInputStream(); 
        //String contentAsString = readIt(is,len); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } finally { 
        //clean up 
        try { 
         os.close(); 
         is.close(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 

        conn.disconnect(); 
       } 
      } 
     }).start(); 
    } 
} 

php

$json = file_get_contents('php://input'); 
$obj = json_decode($json); 
$precio = $obj->{'precio'}; 

Fehler nach Fehlersuche:

08-18 04:28:02.021 31463-31463/com.example.+++.+++W/dalvikvm: VFY: unable to resolve interface method 16209: Landroid/view/Window$Callback;.onProvideKeyboardShortcuts (Ljava/util/List;Landroid/view/Menu;I)V 

08-18 04:28:02.021 31463-31463/com.example.+++.+++W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;) 

08-18 04:28:02.021 31463-31463/com.example.+++.+++W/dalvikvm: VFY: unable to resolve interface method 16211: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z 

08-18 04:28:02.021 31463-31463/com.example.+++.+++W/dalvikvm: VFY: unable to resolve interface method 16215: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode; 

08-18 04:28:02.021 31463-31463/com.example.+++.+++W/dalvikvm: VFY: unable to resolve virtual method 532: Landroid/content/res/TypedArray;.getChangingConfigurations()I 

08-18 04:28:02.021 31463-31463/com.example.+++.+++W/dalvikvm: VFY: unable to resolve virtual method 554: Landroid/content/res/TypedArray;.getType (I)I 

08-18 04:28:02.211 31463-31463/com.example.+++.+++W/dalvikvm: VFY: unable to resolve virtual method 16639: Landroid/widget/FrameLayout;.startActionModeForChild (Landroid/view/View;Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode; 

08-18 04:28:02.271 31463-31463/com.example.+++.+++W/dalvikvm: VFY: unable to resolve virtual method 495: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable; 

08-18 04:28:02.271 31463-31463/com.example.+++.+++W/dalvikvm: VFY: unable to resolve virtual method 497: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable; 

08-18 04:28:02.271 31463-31463/com.example.+++.+++W/dalvikvm: VFY: unable to resolve virtual method 321: Landroid/content/Context;.getColorStateList (I)Landroid/content/res/ColorStateList; 

08-18 04:28:02.651 31463-31463/com.example.+++.+++E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering 

08-18 04:28:02.651 31463-31463/com.example.+++.+++W/dalvikvm: VFY: unable to resolve instanceof 152 (Landroid/graphics/drawable/RippleDrawable;) in Landroid/support/v7/widget/AppCompatImageHelper; 

08-18 04:35:38.731 31463-31463/com.example.+++.+++W/dalvikvm: VFY: unable to resolve virtual method 16912: Landroid/widget/Spinner;.getPopupContext()Landroid/content/Context; 

08-18 04:35:38.731 31463-31463/com.example.+++.+++E/dalvikvm: Could not find class 'android.widget.ThemedSpinnerAdapter', referenced from method android.support.v7.widget.AppCompatSpinner$DropDownAdapter.<init> 

08-18 04:35:38.731 31463-31463/com.example.+++.+++W/dalvikvm: VFY: unable to resolve instanceof 2085 (Landroid/widget/ThemedSpinnerAdapter;) in Landroid/support/v7/widget/AppCompatSpinner$DropDownAdapter; 

08-18 04:37:23.001 31463-31463/com.example.+++.+++W/dalvikvm: VFY: unable to resolve virtual method 196: Landroid/app/Notification$Builder;.setLocalOnly (Z)Landroid/app/Notification$Builder; 

08-18 04:37:23.001 31463-31463/com.example.+++.+++W/dalvikvm: VFY: unable to resolve virtual method 453: Landroid/content/pm/PackageManager;.getPackageInstaller()Landroid/content/pm/PackageInstaller; 

08-18 04:37:32.901 31463-31463/com.example.+++.+++W/dalvikvm: method Landroid/support/v7/widget/ListViewCompat;.lookForSelectablePosition incorrectly overrides package-private method with same name in Landroid/widget/ListView; 

08-18 04:37:32.901 31463-31463/com.example.+++.+++W/dalvikvm: VFY: unable to resolve virtual method 14245: Landroid/support/v7/widget/DropDownListView;.drawableHotspotChanged (FF)V 

08-18 04:37:32.901 31463-31463/com.example.+++.+++W/dalvikvm: VFY: unable to resolve virtual method 15841: Landroid/view/View;.drawableHotspotChanged (FF)V 

08-18 04:37:39.821 31463-31463/com.example.+++.+++W/InputEventReceiver: Attempted to finish an input event but the input event receiver has already been disposed. 

Vielen Dank für Ihre Zeit

+0

Sind Sie 'Post' Daten versuchen? – px06

+0

Ja, ich versuche, die in meiner App generierten Daten in eine PHP-Seite zu posten. – alberzyzz

+0

Was siehst du, wenn du 'echo $ json;' in deinem PHP-Skript verwendest? – simon

Antwort

0

Wenn ich Sie versuchen, richtig zu POST Daten an ein PHP-Seite verstehe Ihre Frage. Erstens empfehle ich die Verwendung von , da es ein wenig einfacher zu verstehen ist und die empfohlene Möglichkeit ist, Hintergrundaufgaben in Android auszuführen.

Zum Veröffentlichen von Daten auf einer PHP-Seite können Sie org.apache.http.client.methods.HttpPost und org.apache.http.client.* verwenden.

In meiner App habe ich es so gemacht, aber ich bin mir nicht ganz sicher, ob dies die richtige Art der Dinge zu tun ist. Dieser Code ist generisch und ist nur meine Implementierung, wie ich die Post-Anfrage an mein PHP-Skript gemacht habe.

class PostRequest extends ASyncTask<String, Void, String>{ 

    @Override 
    protected String doInBackground(String... params){ 
     String data = "test data"; 

     HttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost("http://url.to.post.to"); 

     BasicNameValuePair dataPair = new BasicNameValuePair("data", data); 

     List<NameValuePair> nameValuePairList = new ArrayList<>(); 

     nameValuePairList.add(dataPair); 

     try{ 

      UrlEncodedFormEntity uefe = new UrlEncodedFormEntity(nameValuePairList); 
      httpPost.setEntity(uefe); 

      try { 
       HttpResponse httpResponse = httpClient.execute(httpPost); 

       InputStream inputStream = httpResponse.getEntitiy().getContent(); 


       BufferedReader bufferedReader = new BufferedReader(inputStreamReader); 

       StringBuilder stringBuilder = new StringBuilder(); 

       String bufferedStrChunk = null; 

       while((bufferedStrChunk = bufferedReader.readLine()) != null){ 
        stringBuilder.append(bufferedStrChunk); 
       } 

       return stringBuilder.toString(); 

      } catch(ClientProtocolException cpe){ 
       Log.e("app", "Exception occurred: ", cpe); 
      } catch(IOException e){ 
       Log.e("app", "Exception occurred: ", e); 
      } 
     } catch(UnsupportedEncodingException e){ 
      Log.e("app", "Exception occurred: ", e); 
     } 
     return null; 
    } 
} 

Wo data ist ein erforderliches POST Feld von PHP-Skript auf url.to.post.to

+0

Vielen Dank für Ihre Antwort, aber HttpClient, HttpPost, BasicNameValuePair, etc. sind veraltet. – alberzyzz

+0

Soweit ich weiß und nach [dies] (http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/) kann ich nur sehen, dass 'DefaultHttpClient' veraltet ist.Was ich glaube kann für etwas anderes ersetzt werden. – px06

+0

'HttpClient' wurde in API 22 nicht weiter unterstützt – simon

Verwandte Themen