Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8376417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:25:36+00:00 2026-06-09T15:25:36+00:00

I’m not having good luck moving code from my PC to Android. I am

  • 0

I’m not having good luck moving code from my PC to Android. I am having problems with code that works on my PC but not on my Android phone. I am using the same libraries (GSON) and nothing I am doing should be problem for an Android device. Can someone help me?

Logcat:

08-10 22:39:22.400: E/AndroidRuntime(29153): FATAL EXCEPTION: AsyncTask #1
08-10 22:39:22.400: E/AndroidRuntime(29153): java.lang.RuntimeException: An error occured while executing doInBackground()
08-10 22:39:22.400: E/AndroidRuntime(29153):    at android.os.AsyncTask$3.done(AsyncTask.java:278)
08-10 22:39:22.400: E/AndroidRuntime(29153):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
08-10 22:39:22.400: E/AndroidRuntime(29153):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-10 22:39:22.400: E/AndroidRuntime(29153):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
08-10 22:39:22.400: E/AndroidRuntime(29153):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-10 22:39:22.400: E/AndroidRuntime(29153):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
08-10 22:39:22.400: E/AndroidRuntime(29153):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-10 22:39:22.400: E/AndroidRuntime(29153):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-10 22:39:22.400: E/AndroidRuntime(29153):    at java.lang.Thread.run(Thread.java:856)
08-10 22:39:22.400: E/AndroidRuntime(29153): Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 2
08-10 22:39:22.400: E/AndroidRuntime(29153):    at com.google.gson.Gson.fromJson(Gson.java:806)
08-10 22:39:22.400: E/AndroidRuntime(29153):    at com.google.gson.Gson.fromJson(Gson.java:761)
08-10 22:39:22.400: E/AndroidRuntime(29153):    at com.google.gson.Gson.fromJson(Gson.java:710)
08-10 22:39:22.400: E/AndroidRuntime(29153):    at com.g4apps.json.android.verification.client.JsonVerificationClass.jsonVerificationCall(JsonVerificationClass.java:73)
08-10 22:39:22.400: E/AndroidRuntime(29153):    at com.g4apps.json.android.verification.client.MainActivity$SmpProcessor.doInBackground(MainActivity.java:45)
08-10 22:39:22.400: E/AndroidRuntime(29153):    at com.g4apps.json.android.verification.client.MainActivity$SmpProcessor.doInBackground(MainActivity.java:1)
08-10 22:39:22.400: E/AndroidRuntime(29153):    at android.os.AsyncTask$2.call(AsyncTask.java:264)
08-10 22:39:22.400: E/AndroidRuntime(29153):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-10 22:39:22.400: E/AndroidRuntime(29153):    ... 5 more
08-10 22:39:22.400: E/AndroidRuntime(29153): Caused by: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 2
08-10 22:39:22.400: E/AndroidRuntime(29153):    at com.google.gson.stream.JsonReader.nextString(JsonReader.java:464)
08-10 22:39:22.400: E/AndroidRuntime(29153):    at com.google.gson.internal.bind.TypeAdapters$13.read(TypeAdapters.java:349)
08-10 22:39:22.400: E/AndroidRuntime(29153):    at com.google.gson.internal.bind.TypeAdapters$13.read(TypeAdapters.java:337)
08-10 22:39:22.400: E/AndroidRuntime(29153):    at com.google.gson.Gson.fromJson(Gson.java:795)
08-10 22:39:22.400: E/AndroidRuntime(29153):    ... 12 more

My class:

package com.g4apps.json.android.verification.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import com.google.gson.*;
import com.google.gson.reflect.TypeToken;



// The Return class defines the structure of the json object returned by the web service
public class JsonVerificationClass {

    public static class VerificationData {
        private String Name;
        private String Phone;

        public VerificationData (String name, String smartphone){
            this.Name=name;
            this.Phone=smartphone;
        }

        public String getName() { return Name; }

        public String getSmartPhone() { return Phone; }
    }

    // jsonCall is the actual call to the webservice
    public static String jsonVerificationCall(URI url, String app, VerificationData data) {


        try {
            //We need to json objects. One for the request and one for the response
            //The request object is simple and doesn't require an object class to define it.
            Gson json = new Gson();
            String jsondata= json.toJson(data);

            // We use the same HttpClient and HttpPost commands in both the PC and Android versions.
            // These libraries are included in the Android SDK but must be added for the PC.
            HttpClient client = new DefaultHttpClient();
            HttpPost post2 = new HttpPost(url);

            // We use list to List to make the Post Entities
            List<NameValuePair> namevaluePairs = new ArrayList<NameValuePair>(1);
            namevaluePairs.add(new BasicNameValuePair("app",app));
            namevaluePairs.add(new BasicNameValuePair("data",jsondata.toString()));
            //System.out.println(namevaluePairs.toString());
            post2.setEntity(new UrlEncodedFormEntity(namevaluePairs));
            HttpResponse response = client.execute(post2);

            //Read in the response and rebuild the json string
            BufferedReader rd= new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            StringBuilder myresponse = new StringBuilder();
            String inputline;
            while((inputline = rd.readLine()) !=null) myresponse.append(inputline);

            //Get the type from Return Object so we can remove it from the Json String
            java.lang.reflect.Type StringType = new TypeToken<String>(){}.getType();
            rd.close();
            //System.out.println(myresponse.toString());
            String return2= new Gson().fromJson(myresponse.toString(), StringType);
            return return2;
        }
        catch (UnsupportedEncodingException uee) {
            uee.printStackTrace();
        } 
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

}

My Activity:

package com.g4apps.json.android.verification.client;

import java.net.URI;
import java.net.URISyntaxException;

import com.g4apps.json.android.verification.client.JsonVerificationClass;
import com.g4apps.json.android.verification.client.JsonVerificationClass.VerificationData;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;


public class MainActivity extends Activity {
    private TextView tv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView)findViewById(R.id.TextView01);


       //Start new task and run SmpProcessor (Android 2.3+ prohibits Networking activities from running in main thread.
        SmpProcessor task = new SmpProcessor();
        task.execute();

    }
  // added asynctask though other thread methods can be used
  private class SmpProcessor extends AsyncTask <Void,Void,String> {

            // doInBackground sets up variables and calls jsonCall. multiple to jsoncall can be made from the same thread.
            @Override
            protected String doInBackground(Void... voids){ //String[] doInBackground(Void... voids){

                try {

                    URI url = new URI("http://myservice.com/webservices.php");
                    String app="verify";
                    VerificationData data = new VerificationData("Sam","9055551212");

                    // Make jsonCall with will return Return Object
                    String result = JsonVerificationClass.jsonVerificationCall(url,app,data);


                    return result;
                } catch (URISyntaxException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                //return null;
                return null;
            }
            // Handled the retrieval of the data in onPostExecute so I could output it to the android device through the MainActivity.
            protected void onPostExecute(String result) {


                tv.setText("Response: " + result);


                // Handle or call processing for data here as it needs to be done post html call.
            }
        }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:25:38+00:00Added an answer on June 9, 2026 at 3:25 pm

    Sorry I guess I called the wrong service and it was returning an error message in a different format from what the client expected.

    it was expecting {“String”} and got {“Status”:”String”,”Data”:”String[]”}

    Thanks to Dirk who’s suggestion to look at the json string revealed the answer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
For some reason, after submitting a string like this Jack’s Spindle from a text
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.