Android Open Source - reflect-app Ponty Service






From Project

Back to project page reflect-app.

License

The source code is released under:

Apache License

If you think the Android project reflect-app listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.pontydysgu.webio;
//from   ww w  .j av  a2s . co  m
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.google.gson.Gson;
import com.pontydysgu.data.Answer;
import com.pontydysgu.data.LoginData;
import com.pontydysgu.data.StackArray;

import android.util.Log;

public class PontyService {
  
  
  private static final String WEBSERVICE_BASE_URL = "http://ponty.bluerain.de/index.php?r=";
  
  private final static String GETSTACKS_URL = WEBSERVICE_BASE_URL
      + "api/stackswithquestions";
  
  private final static String SUBMIT_ANSWER_URL = WEBSERVICE_BASE_URL
      + "api/answer";
  
  private static final String TAG = "PontyService";
  
  public LoginData logindata;

  public StackArray getUserStacks() throws IOException {
    /*
    BasicClientCookie2 cookie = new BasicClientCookie2("USERNAME",
        "andreas@vratny.de");
    cookie.setAttribute("USERNAME", "andreas@vratny.de");
    cookie.setAttribute("PASSWORD",
        "7c4a8d09ca3762af61e59520943dc26494f8941b");
*/
    //httpclient.getCookieStore().addCookie(cookie);
    
    // Perform GET Request
    HttpGet request = new HttpGet(GETSTACKS_URL);
    String html = this.performRequest(request);
    
    Gson gson = new Gson();
    StackArray result = gson.fromJson(html, StackArray.class);
    Log.i(TAG, "StackArray: "+result);
    return result;
  }
    
  private String performRequest(HttpRequestBase request) throws IOException {
    DefaultHttpClient httpclient = new DefaultHttpClient();

    String username=logindata.getUsername();
    String password=logindata.getPassword();
    
    request.addHeader("Cookie", "USERNAME="+username+";PASSWORD="+password); // TOOD: Could be better
    HttpResponse response = httpclient.execute(request);
    HttpEntity responseEntity = response.getEntity();
    if (responseEntity != null) {
      if (response.getStatusLine().getStatusCode() != 200) {
        String html = EntityUtils.toString(responseEntity);
        Log.i(TAG, "URL "+request.getURI()+"\nHTTP Response: "+html);
        throw new IOException("Unexpected HTTP Status Code: "+response.getStatusLine().getStatusCode());
      }
      
      String html = EntityUtils.toString(responseEntity);
      return html;
      
    } else {
      throw new IOException("Couldn't retrieve webservice content! URL: "+request.getURI());
    }    
  }

  public void sendAnswer(Answer answer) throws IOException {
    if (answer.getQuestionId() == null) throw new IOException("QuestionID is null!");
    Log.i(TAG, "Submitting answer: "+answer);
    HttpPost request = new HttpPost(SUBMIT_ANSWER_URL+"&questionid="+answer.getQuestionId());

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("answer", answer.getAnswerText()));
        request.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    this.performRequest(request);
  }
}




Java Source Code List

com.google.gson.DefaultDateTypeAdapter.java
com.google.gson.ExclusionStrategy.java
com.google.gson.FieldAttributes.java
com.google.gson.FieldNamingPolicy.java
com.google.gson.FieldNamingStrategy.java
com.google.gson.GsonBuilder.java
com.google.gson.Gson.java
com.google.gson.InstanceCreator.java
com.google.gson.JsonArray.java
com.google.gson.JsonDeserializationContext.java
com.google.gson.JsonDeserializer.java
com.google.gson.JsonElement.java
com.google.gson.JsonIOException.java
com.google.gson.JsonNull.java
com.google.gson.JsonObject.java
com.google.gson.JsonParseException.java
com.google.gson.JsonParser.java
com.google.gson.JsonPrimitive.java
com.google.gson.JsonSerializationContext.java
com.google.gson.JsonSerializer.java
com.google.gson.JsonStreamParser.java
com.google.gson.JsonSyntaxException.java
com.google.gson.LongSerializationPolicy.java
com.google.gson.TreeTypeAdapter.java
com.google.gson.TypeAdapterFactory.java
com.google.gson.TypeAdapter.java
com.google.gson.annotations.Expose.java
com.google.gson.annotations.SerializedName.java
com.google.gson.annotations.Since.java
com.google.gson.annotations.Until.java
com.google.gson.annotations.package-info.java
com.google.gson.internal.ConstructorConstructor.java
com.google.gson.internal.Excluder.java
com.google.gson.internal.JsonReaderInternalAccess.java
com.google.gson.internal.LazilyParsedNumber.java
com.google.gson.internal.LinkedHashTreeMap.java
com.google.gson.internal.ObjectConstructor.java
com.google.gson.internal.Primitives.java
com.google.gson.internal.Streams.java
com.google.gson.internal.UnsafeAllocator.java
com.google.gson.internal.$Gson$Preconditions.java
com.google.gson.internal.$Gson$Types.java
com.google.gson.internal.bind.ArrayTypeAdapter.java
com.google.gson.internal.bind.CollectionTypeAdapterFactory.java
com.google.gson.internal.bind.DateTypeAdapter.java
com.google.gson.internal.bind.JsonTreeReader.java
com.google.gson.internal.bind.JsonTreeWriter.java
com.google.gson.internal.bind.MapTypeAdapterFactory.java
com.google.gson.internal.bind.ObjectTypeAdapter.java
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.java
com.google.gson.internal.bind.SqlDateTypeAdapter.java
com.google.gson.internal.bind.TimeTypeAdapter.java
com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.java
com.google.gson.internal.bind.TypeAdapters.java
com.google.gson.internal.package-info.java
com.google.gson.reflect.TypeToken.java
com.google.gson.reflect.package-info.java
com.google.gson.stream.JsonReader.java
com.google.gson.stream.JsonScope.java
com.google.gson.stream.JsonToken.java
com.google.gson.stream.JsonWriter.java
com.google.gson.stream.MalformedJsonException.java
com.google.gson.package-info.java
com.pontydysgu.data.Answer.java
com.pontydysgu.data.LoginData.java
com.pontydysgu.data.QuestionStack.java
com.pontydysgu.data.Question.java
com.pontydysgu.data.StackArray.java
com.pontydysgu.gui.StackArrayAdapter.java
com.pontydysgu.pontylearningapp.DataService.java
com.pontydysgu.pontylearningapp.Login.java
com.pontydysgu.pontylearningapp.QuestionCycle.java
com.pontydysgu.pontylearningapp.Stackoverview.java
com.pontydysgu.webio.GetWebRequest.java
com.pontydysgu.webio.LoginService.java
com.pontydysgu.webio.PontyService.java
com.pontydysgu.webio.RetrieveStacksCallback.java
com.pontydysgu.webio.RetrieveStacksTask.java
com.pontydysgu.webio.SubmitAnswerCallback.java
com.pontydysgu.webio.SubmitAnswerTask.java