Skip to content
This repository has been archived by the owner on Sep 17, 2022. It is now read-only.

How to Handle JsonSyntaxException when Receiving Response from GAE #64

Open
troy21688 opened this issue Jan 28, 2016 · 1 comment
Open

Comments

@troy21688
Copy link

I made a previous post and appear to have made progress, but please see the StackOverflow post for reference: http://stackoverflow.com/questions/35068601/how-to-handle-jsonsyntaxexception-when-receiving-response-from-gae

I am trying to replicate the Google App Engine Servlets module [here][1] using Retrofit instead of AsyncTask.

I find it odd, but apparently Retrofit 2.0 does not support connections to Google App Engine, as stated [here][2] in the issues of the GitHub repository.

As a result, I am using Retrofit 1.9 and OkHttp 2.3 dependencies.

I have created a project called "Retrofit Test" in the Google Developer Console, and Google has supplied me with a URL for the project: "http://retrofit-test-1203.appspot.com" with the subdomain as "http://retrofit-test-1203.appspot.com/hello. These will be my respective URL's for Retrofit. Below is my code:

Gradle:

 apply plugin: 'com.android.application'

    android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "com.troychuinard.retrofittest"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
     } 
    }

    dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile 'com.squareup.retrofit:retrofit:1.9.0'
    compile 'com.squareup.okhttp:okhttp:2.3.0'


    }


MainActivity:

```
public class MainActivity extends AppCompatActivity {

//set the URL of the server, as defined in the Google Servlets Module Documentation
private static String PROJECT_URL = "http://retrofit-test-1203.appspot.com";
private Button mTestButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    mTestButton = (Button) findViewById(R.id.test_button);



    //Instantiate a new UserService object, and call the "testRequst" method, created in the interface
    //to interact with the server
    mTestButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Instantiate a new RestAdapter Object, setting the endpoint as the URL of the server
            RestAdapter restAdapter = new RestAdapter.Builder()
                    .setEndpoint(PROJECT_URL)
                    .setClient(new OkClient(new OkHttpClient()))
                    .build();

            UserService userService = restAdapter.create(UserService.class);
            userService.testRequest("Test_Name", new Callback<String>() {
                @Override
                public void success(String s, Response response) {
                    Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();

                }

                @Override
                public void failure(RetrofitError error) {
                    Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
                }
            });
        }
    });


   }
}

UserService (Retrofit)

```
public interface UserService {

@POST("/hello")
void testRequest(@Query("name") String name, Callback<String> cb);

}

My Servlet:

```
public class MyServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {
    resp.setContentType("text/plain");
    resp.getWriter().println("Please use the form to POST to this url");
}

@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {
    String name = req.getParameter("name");
    resp.setContentType("text/plain");
    if(name == null) {
        resp.getWriter().println("Please enter a name");
    }
    resp.getWriter().println("Hello " + name);
  }
}

As you can see, I have set the project up so that I make a server request on a button click. Similar to the Google App Engine Servlets module, I am expecting to receive a response from the Server, which I then display as a Toast that says "Hello + (whatever parameter entered into the .testRequest() method, which is "Test_Name" in this case.

@loosebazooka
Copy link
Contributor

I think the correct forums for this would be someone more familiar with retrofit and how it might handle toast creation or other problems... However to help you debug you can look at your server side logs. Perhaps that will give you some insight into whether you are even sending a request. Check the google developer console logging to see what requests are coming in.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants