Community
    • Categories
    • Recent
    • Popular
    • Users
    • Search
    • Register
    • Login
    1. Home
    2. FSchmid
    F
    Offline
    • Profile
    • Following 0
    • Followers 0
    • Topics 1
    • Posts 1
    • Groups 0

    FSchmid

    @FSchmid

    0
    Reputation
    21
    Profile views
    1
    Posts
    0
    Followers
    0
    Following
    Joined
    Last Online
    Age 24

    FSchmid Unfollow Follow

    Latest posts made by FSchmid

    • Android App Entwicklung idoit

      Hallo,

      für mein Studium (Projektarbeit/Bachelorarbeit) habe ich die Aufgabe, eine Applikation für Android zu schreiben, mit der man auf die Datenbank i-doit zugreifen kann. Dies ist mein erstes Projekt im Bezug auf Android und JSON. Das Dokument mit der technischen Erläuterung der "i-doit JSON RPC API" hilft mir leider nur bedingt weiter. Ich versuche per HTTP Post auf die demo-Datenbank zuzugreifen, jedoch weiß ich nicht, in wiefern ich die params und den api-key bzw. Nick/PW einbinden soll. Ebenfalls weiß ich nicht, ob ich die richtige URL verwende, da ich ständig als Request-Objekt einen Text mit "400 Bad Request"-Nachricht bekomme.

      Folgendermaßen sieht mein Code aus:

      
      @Override
      protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_startscreen);
      
              //Aufruf der execute-Methode (Ist die übergebene URL  richtig?)
              new DownloadTextTask().execute("http://demo.i-doit.com/src/jsonrpc.php");
      }
      
      public InputStream OpenHttpPOSTConnection(String url){
              InputStream inputStream = null;
              HttpClient httpClient = new DefaultHttpClient();
              try{
      
                  HttpPost httpPost = new HttpPost(url);
      
                  // HTTP Post Header (Was muss alles per Header mitgegeben werden und stimmt die URL?)
                  httpPost.addHeader("Host","http://demo.i-doit.com/src/jsonrpc.php");
                  httpPost.addHeader("Content-Type", "application/json-rpc");
                  httpPost.addHeader("X-RPC-Auth-Username","admin");
                  httpPost.addHeader("X-RPC-Auth-Password","admin");
      
                  // JSON Objekt mit Schlüsselwertpaaren (Sind die SWP vollständig und korrekt?)
                  JSONObject json = new JSONObject();
                  HashMap <string, string="">map = new HashMap();
                  map.put("apikey","c1ia5q");
                  map.put("objID","4438");
                  map.put("catgID", "41");
                  json.put("method","cmdb.category");
                  json.put("version","2.0");
                  json.put("id","1");
                  json.put("params",map);
      
                  // JSON Objekt an HTTP Post übergeben
                  StringEntity params =new StringEntity(json.toString());
                  httpPost.setEntity(params);
      
                  HttpResponse httpResponse = httpClient.execute(httpPost);
                  inputStream = httpResponse.getEntity().getContent();
      
              }catch(Exception e){
                  Log.d("OpenHttpPOSTConnection", e.getLocalizedMessage());
              }finally {
                  httpClient.getConnectionManager().shutdown();
              }
              return inputStream;
          }
      
          private String DownloadText(String URL){
              int BUFFER_SIZE = 2000;
              InputStream in = null;
              try{
                  in = OpenHttpPOSTConnection(URL);
              }catch (Exception e){
                  Log.d("Networking", e.getLocalizedMessage());
                  return "";
              }
      
              InputStreamReader isr = new InputStreamReader(in);
              int charRead;
              String str = "";
              char[] inputBuffer = new char[BUFFER_SIZE];
              try{
                  while ((charRead = isr.read(inputBuffer))>0){
                      String readString = String.copyValueOf(inputBuffer, 0, charRead);
                      str += readString;
                      inputBuffer = new char[BUFFER_SIZE];
                  }
                  in.close();
              }catch (IOException e){
                  Log.d("DownloadText", e.getLocalizedMessage());
                  return "";
              }
              return str;
          }
      
          private class DownloadTextTask extends AsyncTask<string, void,="" string="">{
              protected String doInBackground(String... urls){
                  return DownloadText(urls[0]);
              }
      
              @Override
              protected void onPostExecute(String result) {
                  // Ausgabe des JSON-Objekts vom Server als String im Textfeld der Android-App
                  EditText editText = (EditText)findViewById(R.id.editText_output);
                  editText.setText(result);
              }
          }</string,></string,> 
      

      Ich wäre sehr dankbar, wenn mir jemand zeigen könnte, wo meine Fehler liegen.

      posted in Betrieb
      F
      FSchmid