Karsten Voigt
- Forum-Beiträge: 2
16.07.2013, 19:10:38 via Website
16.07.2013 19:10:38 via Website
Hallo an alle,
vielleicht kann mir mal jmd helfen ich diese Zeile an eine aspx Seite senden :
login.aspx?jsonString={"did":1000,"password":"3141J3131627I","login":"LAppBenutzer3"}
Leider sind alle meine bisherigen Bemühungen umsonst, den ich bekomme immer eine Fehlermeldung zurück (vom Webservice das der Syntax falsch ist)
hier meine Bemühungen
API.java
MainActivity.java
also eclipse zeigt keine Fehler nur der webservice.
alle urls sind eigentl vollständig aber ich darf als Neuling keine Urls schreiben.
Bin für alles dankbar
vielleicht kann mir mal jmd helfen ich diese Zeile an eine aspx Seite senden :
login.aspx?jsonString={"did":1000,"password":"3141J3131627I","login":"LAppBenutzer3"}
Leider sind alle meine bisherigen Bemühungen umsonst, den ich bekomme immer eine Fehlermeldung zurück (vom Webservice das der Syntax falsch ist)
hier meine Bemühungen
API.java
1import java.io.BufferedReader;
2import java.io.IOException;
3import java.io.InputStream;
4import java.io.InputStreamReader;
5import java.nio.charset.Charset;
6
7import org.apache.http.HttpEntity;
8import org.apache.http.HttpResponse;
9import org.apache.http.client.methods.HttpPost;
10import org.apache.http.entity.StringEntity;
11import org.apache.http.impl.client.DefaultHttpClient;
12import org.apache.http.message.BasicHeader;
13import org.apache.http.protocol.HTTP;
14
15import org.json.JSONObject;
16
17
18import android.util.Log;
19
20public class API {
21
22 private static final String TAG = "API";
23
24 public static JSONObject SendHttpPost(String URL, JSONObject jsonObjSend){
25 try {
26 DefaultHttpClient httpclient = new DefaultHttpClient();
27 HttpPost httpPostRequest = new HttpPost(URL);
28
29 StringEntity se;
30 se = new StringEntity(jsonObjSend.toString());
31 se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
32 se.setContentType("application/json; charset=UTF-8");
33 httpPostRequest.setHeader("Content-type","application/json");
34 Log.i(TAG,se.toString());
35
36 // HTTP Parameter
37
38 httpPostRequest.setEntity(se);
39
40 long t = System.currentTimeMillis();
41 HttpResponse response = httpclient.execute(httpPostRequest);
42 Log.i(TAG, "Antwort in [" + (System.currentTimeMillis()-t) + "ms]");
43
44 HttpEntity entity = response.getEntity();
45
46 if (entity != null) {
47 InputStream instream = entity.getContent();
48 org.apache.http.Header contentEncoding = response.getFirstHeader("Content-encoding");
49 String resultString = convertStreamToString(instream);
50 instream.close();
51
52 JSONObject jsonObjRecv = new JSONObject(resultString);
53 Log.i(TAG,"<JSONObject>\n"+jsonObjRecv.toString()+"\n</JSONObject>");
54
55 return jsonObjRecv;
56 }
57 }
58 catch (Exception e){
59 e.printStackTrace();
60 }
61 return null;
62 }
63
64 private static String convertStreamToString(InputStream is) {
65
66 BufferedReader reader = new BufferedReader(new InputStreamReader(is));
67 StringBuilder sb = new StringBuilder();
68
69 String line = null;
70 try {
71 while ((line = reader.readLine()) != null) {
72 sb.append(line + "\n");
73 }
74 } catch (IOException e) {
75 e.printStackTrace();
76 } finally {
77 try {
78 is.close();
79 } catch (IOException e) {
80 e.printStackTrace();
81 }
82 }
83 return sb.toString();
84 }
85}
2import java.io.IOException;
3import java.io.InputStream;
4import java.io.InputStreamReader;
5import java.nio.charset.Charset;
6
7import org.apache.http.HttpEntity;
8import org.apache.http.HttpResponse;
9import org.apache.http.client.methods.HttpPost;
10import org.apache.http.entity.StringEntity;
11import org.apache.http.impl.client.DefaultHttpClient;
12import org.apache.http.message.BasicHeader;
13import org.apache.http.protocol.HTTP;
14
15import org.json.JSONObject;
16
17
18import android.util.Log;
19
20public class API {
21
22 private static final String TAG = "API";
23
24 public static JSONObject SendHttpPost(String URL, JSONObject jsonObjSend){
25 try {
26 DefaultHttpClient httpclient = new DefaultHttpClient();
27 HttpPost httpPostRequest = new HttpPost(URL);
28
29 StringEntity se;
30 se = new StringEntity(jsonObjSend.toString());
31 se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
32 se.setContentType("application/json; charset=UTF-8");
33 httpPostRequest.setHeader("Content-type","application/json");
34 Log.i(TAG,se.toString());
35
36 // HTTP Parameter
37
38 httpPostRequest.setEntity(se);
39
40 long t = System.currentTimeMillis();
41 HttpResponse response = httpclient.execute(httpPostRequest);
42 Log.i(TAG, "Antwort in [" + (System.currentTimeMillis()-t) + "ms]");
43
44 HttpEntity entity = response.getEntity();
45
46 if (entity != null) {
47 InputStream instream = entity.getContent();
48 org.apache.http.Header contentEncoding = response.getFirstHeader("Content-encoding");
49 String resultString = convertStreamToString(instream);
50 instream.close();
51
52 JSONObject jsonObjRecv = new JSONObject(resultString);
53 Log.i(TAG,"<JSONObject>\n"+jsonObjRecv.toString()+"\n</JSONObject>");
54
55 return jsonObjRecv;
56 }
57 }
58 catch (Exception e){
59 e.printStackTrace();
60 }
61 return null;
62 }
63
64 private static String convertStreamToString(InputStream is) {
65
66 BufferedReader reader = new BufferedReader(new InputStreamReader(is));
67 StringBuilder sb = new StringBuilder();
68
69 String line = null;
70 try {
71 while ((line = reader.readLine()) != null) {
72 sb.append(line + "\n");
73 }
74 } catch (IOException e) {
75 e.printStackTrace();
76 } finally {
77 try {
78 is.close();
79 } catch (IOException e) {
80 e.printStackTrace();
81 }
82 }
83 return sb.toString();
84 }
85}
MainActivity.java
1import org.json.JSONException;
2import org.json.JSONObject;
3
4import android.os.Bundle;
5import android.util.Log;
6import android.view.View;
7import android.widget.EditText;
8import android.app.Activity;
9
10
11
12
13public class MainActivity extends Activity {
14
15 private static final String TAG = "MainActivity";
16 private static final String URLLogin = "/login.aspx";
17
18
19 @Override
20 protected void onCreate(Bundle savedInstanceState) {
21 super.onCreate(savedInstanceState);
22 setContentView(R.layout.activity_main);
23
24 }
25
26 public void pruefeLogin(View view){
27
28
29 int userdid = (int) (1000+Math.random()*((99999999-1000)+1));
30 JSONObject jsonObjSend = new JSONObject();
31 EditText username =(EditText)findViewById(R.id.login_name);
32 Log.i(TAG,username.getText().toString());
33 EditText userpass =(EditText)findViewById(R.id.login_pass);
34
35 try {
36 jsonObjSend.put("did",userdid);
37 jsonObjSend.put("login",username.getText().toString());
38 jsonObjSend.put("passwort",userpass.getText().toString());
39
40
41 Log.i(TAG,jsonObjSend.toString());
42
43 }catch (JSONException e) {
44 e.printStackTrace();
45 }
46
47 JSONObject jsonObjRecv =API.SendHttpPost(URLLogin, jsonObjSend);
48
49
50
51 }
52
53}
2import org.json.JSONObject;
3
4import android.os.Bundle;
5import android.util.Log;
6import android.view.View;
7import android.widget.EditText;
8import android.app.Activity;
9
10
11
12
13public class MainActivity extends Activity {
14
15 private static final String TAG = "MainActivity";
16 private static final String URLLogin = "/login.aspx";
17
18
19 @Override
20 protected void onCreate(Bundle savedInstanceState) {
21 super.onCreate(savedInstanceState);
22 setContentView(R.layout.activity_main);
23
24 }
25
26 public void pruefeLogin(View view){
27
28
29 int userdid = (int) (1000+Math.random()*((99999999-1000)+1));
30 JSONObject jsonObjSend = new JSONObject();
31 EditText username =(EditText)findViewById(R.id.login_name);
32 Log.i(TAG,username.getText().toString());
33 EditText userpass =(EditText)findViewById(R.id.login_pass);
34
35 try {
36 jsonObjSend.put("did",userdid);
37 jsonObjSend.put("login",username.getText().toString());
38 jsonObjSend.put("passwort",userpass.getText().toString());
39
40
41 Log.i(TAG,jsonObjSend.toString());
42
43 }catch (JSONException e) {
44 e.printStackTrace();
45 }
46
47 JSONObject jsonObjRecv =API.SendHttpPost(URLLogin, jsonObjSend);
48
49
50
51 }
52
53}
also eclipse zeigt keine Fehler nur der webservice.
alle urls sind eigentl vollständig aber ich darf als Neuling keine Urls schreiben.
Bin für alles dankbar
— geändert am 16.07.2013, 19:12:05
Empfohlener redaktioneller Inhalt
Mit Deiner Zustimmung wird hier ein externer Inhalt geladen.
Mit Klick auf den oben stehenden Button erklärst Du Dich damit einverstanden, dass Dir externe Inhalte angezeigt werden dürfen. Dabei können personenbezogene Daten an Drittanbieter übermittelt werden. Mehr Infos dazu findest Du in unserer Datenschutzerklärung.